@workday/canvas-kit-react 9.0.0-alpha.344-next.3 → 9.0.0-alpha.350-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/common/lib/styles/focusRing.ts +0 -9
  2. package/common/lib/theming/createCanvasTheme.ts +39 -42
  3. package/common/lib/theming/getObjectProxy.ts +31 -19
  4. package/common/lib/theming/useTheme.ts +1 -2
  5. package/common/lib/utils/deepMerge.ts +21 -0
  6. package/common/lib/utils/memoize.ts +23 -0
  7. package/dist/commonjs/common/lib/styles/focusRing.d.ts +0 -3
  8. package/dist/commonjs/common/lib/styles/focusRing.d.ts.map +1 -1
  9. package/dist/commonjs/common/lib/styles/focusRing.js +2 -16
  10. package/dist/commonjs/common/lib/theming/createCanvasTheme.d.ts +7 -6
  11. package/dist/commonjs/common/lib/theming/createCanvasTheme.d.ts.map +1 -1
  12. package/dist/commonjs/common/lib/theming/createCanvasTheme.js +34 -30
  13. package/dist/commonjs/common/lib/theming/getObjectProxy.d.ts +9 -3
  14. package/dist/commonjs/common/lib/theming/getObjectProxy.d.ts.map +1 -1
  15. package/dist/commonjs/common/lib/theming/getObjectProxy.js +20 -20
  16. package/dist/commonjs/common/lib/theming/useTheme.d.ts.map +1 -1
  17. package/dist/commonjs/common/lib/theming/useTheme.js +2 -5
  18. package/dist/commonjs/common/lib/utils/deepMerge.d.ts +2 -0
  19. package/dist/commonjs/common/lib/utils/deepMerge.d.ts.map +1 -0
  20. package/dist/commonjs/common/lib/utils/deepMerge.js +24 -0
  21. package/dist/commonjs/common/lib/utils/memoize.d.ts +6 -0
  22. package/dist/commonjs/common/lib/utils/memoize.d.ts.map +1 -0
  23. package/dist/commonjs/common/lib/utils/memoize.js +24 -0
  24. package/dist/commonjs/side-panel/lib/SidePanel.d.ts.map +1 -1
  25. package/dist/commonjs/side-panel/lib/SidePanel.js +1 -2
  26. package/dist/commonjs/tokens/lib/type/fontFamilies.d.ts.map +1 -1
  27. package/dist/commonjs/tokens/lib/type/fontFamilies.js +5 -6
  28. package/dist/commonjs/tooltip/lib/useTooltip.d.ts.map +1 -1
  29. package/dist/commonjs/tooltip/lib/useTooltip.js +16 -11
  30. package/dist/es6/common/lib/styles/focusRing.d.ts +0 -3
  31. package/dist/es6/common/lib/styles/focusRing.d.ts.map +1 -1
  32. package/dist/es6/common/lib/styles/focusRing.js +1 -12
  33. package/dist/es6/common/lib/theming/createCanvasTheme.d.ts +7 -6
  34. package/dist/es6/common/lib/theming/createCanvasTheme.d.ts.map +1 -1
  35. package/dist/es6/common/lib/theming/createCanvasTheme.js +32 -27
  36. package/dist/es6/common/lib/theming/getObjectProxy.d.ts +9 -3
  37. package/dist/es6/common/lib/theming/getObjectProxy.d.ts.map +1 -1
  38. package/dist/es6/common/lib/theming/getObjectProxy.js +20 -20
  39. package/dist/es6/common/lib/theming/useTheme.d.ts.map +1 -1
  40. package/dist/es6/common/lib/theming/useTheme.js +2 -2
  41. package/dist/es6/common/lib/utils/deepMerge.d.ts +2 -0
  42. package/dist/es6/common/lib/utils/deepMerge.d.ts.map +1 -0
  43. package/dist/es6/common/lib/utils/deepMerge.js +20 -0
  44. package/dist/es6/common/lib/utils/memoize.d.ts +6 -0
  45. package/dist/es6/common/lib/utils/memoize.d.ts.map +1 -0
  46. package/dist/es6/common/lib/utils/memoize.js +20 -0
  47. package/dist/es6/side-panel/lib/SidePanel.d.ts.map +1 -1
  48. package/dist/es6/side-panel/lib/SidePanel.js +1 -2
  49. package/dist/es6/tokens/lib/type/fontFamilies.d.ts.map +1 -1
  50. package/dist/es6/tokens/lib/type/fontFamilies.js +5 -3
  51. package/dist/es6/tooltip/lib/useTooltip.d.ts.map +1 -1
  52. package/dist/es6/tooltip/lib/useTooltip.js +16 -11
  53. package/package.json +3 -4
  54. package/side-panel/lib/SidePanel.tsx +1 -2
  55. package/tokens/lib/type/fontFamilies.ts +5 -4
  56. package/tooltip/lib/useTooltip.tsx +18 -11
@@ -1,7 +1,6 @@
1
1
  import {keyframes, Theme, CSSObject} from '@emotion/react';
2
2
  import {canvas} from '@workday/canvas-kit-react/tokens';
3
3
  import {defaultCanvasTheme} from '../theming/index';
4
- import memoize from 'lodash/memoize';
5
4
 
6
5
  interface FocusRingOptions {
7
6
  width?: number;
@@ -16,7 +15,6 @@ interface FocusRingOptions {
16
15
  inset?: 'inner' | 'outer';
17
16
  innerColor?: string;
18
17
  outerColor?: string;
19
- memoize?: boolean;
20
18
  }
21
19
 
22
20
  function calculateFocusRing({
@@ -62,8 +60,6 @@ function calculateFocusRing({
62
60
  return {boxShadow};
63
61
  }
64
62
 
65
- export const memoizedFocusRing = memoize(calculateFocusRing, (...args) => JSON.stringify(args));
66
-
67
63
  /**
68
64
  * A utility to create a canvas style focus ring around your widget.
69
65
  * By default, this mixin will create a 2px focus ring tightly wrapped
@@ -96,7 +92,6 @@ export function focusRing(options: FocusRingOptions = {}, theme?: Theme): CSSObj
96
92
  outerColor = theme && theme.canvas
97
93
  ? theme.canvas.palette.common.focusOutline
98
94
  : defaultCanvasTheme.palette.common.focusOutline,
99
- memoize = true,
100
95
  inset,
101
96
  } = options;
102
97
 
@@ -109,9 +104,5 @@ export function focusRing(options: FocusRingOptions = {}, theme?: Theme): CSSObj
109
104
  inset,
110
105
  };
111
106
 
112
- if (memoize) {
113
- return memoizedFocusRing(args);
114
- }
115
-
116
107
  return calculateFocusRing(args);
117
108
  }
@@ -1,7 +1,4 @@
1
1
  import chroma from 'chroma-js';
2
- import merge from 'lodash/merge';
3
- import memoize from 'lodash/memoize';
4
- import findKey from 'lodash/findKey';
5
2
  import colors from '@workday/canvas-colors-web';
6
3
  import {defaultCanvasTheme} from './theme';
7
4
  import {
@@ -14,40 +11,43 @@ import {
14
11
  import {CanvasColor} from '@workday/canvas-kit-react/tokens';
15
12
  import {pickForegroundColor} from '../utils';
16
13
 
17
- export enum ColorDirection {
18
- Darken,
19
- Brighten,
20
- }
21
-
22
- export function shiftColor(hexColor: string, direction: ColorDirection) {
23
- const canvasColor = findKey(colors, color => color === hexColor);
14
+ // Should these be exported?
15
+ import { deepMerge } from '../utils/deepMerge';
16
+ import { memoize } from '../utils/memoize';
24
17
 
25
- const darken = direction === ColorDirection.Darken;
18
+ export const shiftColor = memoize(
19
+ (hexColor: string, amount = 100) => {
20
+ const canvasColor = Object.keys(colors).find(color => colors[color] === hexColor);
26
21
 
27
- if (canvasColor) {
28
- const colorRegex = /([a-zAz]*)(\d{3})/g;
29
- const match = colorRegex.exec(canvasColor);
22
+ if (canvasColor) {
23
+ const colorRegex = /([a-zAz]*)(\d{3})/g;
24
+ const match = colorRegex.exec(canvasColor);
30
25
 
31
- if (match) {
32
- const baseColor = match[1];
33
- const shadeNumber = parseInt(match[2], 10);
26
+ if (match) {
27
+ const baseColor = match[1];
28
+ const shadeNumber = parseInt(match[2], 10);
34
29
 
35
- const newShade = darken ? shadeNumber + 100 : shadeNumber - 100;
30
+ const newShade = shadeNumber + amount;
36
31
 
37
- if (newShade >= 100 && newShade <= 600) {
38
- return colors[(baseColor + newShade) as CanvasColor];
32
+ if (newShade >= 100 && newShade <= 600) {
33
+ return colors[(baseColor + newShade) as CanvasColor];
34
+ }
39
35
  }
40
36
  }
41
- }
42
37
 
43
- try {
44
- const newColor = darken ? chroma(hexColor).darken() : chroma(hexColor).brighten();
45
- return newColor.hex();
46
- } catch (e) {
47
- console.warn(`Invalid color '${hexColor}' used in theme`);
48
- return hexColor;
49
- }
50
- }
38
+ try {
39
+ const newColor =
40
+ amount > 0
41
+ ? chroma(hexColor).darken(amount / 100)
42
+ : chroma(hexColor).brighten(-amount / 100);
43
+ return newColor.hex();
44
+ } catch (e) {
45
+ console.warn(`Invalid color '${hexColor}' used in theme`);
46
+ return hexColor;
47
+ }
48
+ },
49
+ (color, value) => `${color}.${value}`
50
+ );
51
51
 
52
52
  function fillPalette(
53
53
  defaultPalette: CanvasThemePalette,
@@ -59,21 +59,13 @@ function fillPalette(
59
59
 
60
60
  const main = palette.main || defaultPalette.main;
61
61
  const dark =
62
- palette.dark ||
63
- (palette.main && shiftColor(palette.main, ColorDirection.Darken)) ||
64
- defaultPalette.dark;
62
+ palette.dark || (palette.main && shiftColor(palette.main, 100)) || defaultPalette.dark;
65
63
  const darkest =
66
- palette.darkest ||
67
- (palette.main && shiftColor(dark, ColorDirection.Darken)) ||
68
- defaultPalette.darkest;
64
+ palette.darkest || (palette.main && shiftColor(palette.main, 200)) || defaultPalette.darkest;
69
65
  const light =
70
- palette.light ||
71
- (palette.main && shiftColor(palette.main, ColorDirection.Brighten)) ||
72
- defaultPalette.light;
66
+ palette.light || (palette.main && shiftColor(palette.main, -100)) || defaultPalette.light;
73
67
  const lightest =
74
- palette.lightest ||
75
- (palette.main && shiftColor(light, ColorDirection.Brighten)) ||
76
- defaultPalette.lightest;
68
+ palette.lightest || (palette.main && shiftColor(palette.main, -200)) || defaultPalette.lightest;
77
69
  const contrast = palette.contrast || pickForegroundColor(main) || defaultPalette.contrast;
78
70
 
79
71
  return {
@@ -103,7 +95,12 @@ function calculateCanvasTheme(partialTheme: PartialCanvasTheme): CanvasTheme {
103
95
  direction: direction === ContentDirection.RTL ? direction : ContentDirection.LTR,
104
96
  };
105
97
 
106
- return merge({}, defaultCanvasTheme, mergeable, extraFields) as CanvasTheme;
98
+ return deepMerge(deepMerge({}, defaultCanvasTheme), {...mergeable, ...extraFields});
107
99
  }
108
100
 
101
+ /**
102
+ * Creates a full {@link CanvasTheme} from any partial theme by deeply merging with the
103
+ * `defaultCanvasTheme` object. The function is memoized, but it is best to run this function
104
+ * only once and save the result.
105
+ */
109
106
  export const createCanvasTheme = memoize(calculateCanvasTheme, (...args) => JSON.stringify(args));
@@ -1,25 +1,37 @@
1
+ /**
2
+ * For custom themes that do not overwrite every default.
3
+ */
4
+ export type RecursivePartial<T> = {
5
+ [P in keyof T]?: RecursivePartial<T[P]>;
6
+ };
7
+
1
8
  /**
2
9
  * The functions gets a new object proxy. This is used for theme to ensure regardless of a partial theme object
3
10
  * there will be a fallback theme object so that properties aren't undefined regardless of the `target` object.
4
11
  * This function will also handle nested properties.
5
- * @param target Any custom theme object
6
- * @param fallback A fallback theme object
12
+ * @param target A partial object with up to the same shape as the `fallback` object
13
+ * @param fallback A fallback object. If a property to be accessed from the proxy is not available on the target object, the fallback object will be used
7
14
  */
8
- /* eslint-disable compat/compat */
9
- export function getObjectProxy<T extends {}>(target: unknown, fallback: T): T {
10
- return new Proxy(target as any, {
11
- get(target, p: string) {
12
- const prop = p as keyof T; // keep Typescript happy - the `get` signature cannot contain `keyof T`
13
- if (typeof fallback[prop] === 'object' && typeof target[prop] === 'object') {
14
- return getObjectProxy(target[prop] || {}, fallback[prop]);
15
- } else if (target[prop] !== undefined) {
16
- return target[prop];
17
- } else {
18
- return fallback[prop];
19
- }
20
- },
21
- ownKeys(target) {
22
- return Object.keys(fallback);
23
- },
24
- }) as T;
15
+ export function getObjectProxy<T extends {}>(
16
+ target: RecursivePartial<T> | undefined,
17
+ fallback: T
18
+ ): T {
19
+ if (target) {
20
+ return new Proxy(target, {
21
+ get<T>(target: T, prop: string | symbol, receiver: any) {
22
+ if (typeof (fallback as any)[prop] === 'object') {
23
+ return getObjectProxy(target[prop as keyof T], (fallback as any)[prop]);
24
+ }
25
+ const targetProp = target[prop as keyof T];
26
+ if (targetProp !== undefined) {
27
+ return targetProp;
28
+ }
29
+ return Reflect.get(fallback, prop, receiver);
30
+ },
31
+ ownKeys() {
32
+ return Reflect.ownKeys(fallback);
33
+ },
34
+ }) as T;
35
+ }
36
+ return fallback;
25
37
  }
@@ -1,4 +1,3 @@
1
- import get from 'lodash/get';
2
1
  import {useTheme as useEmotionTheme} from '@emotion/react';
3
2
  import {
4
3
  defaultCanvasTheme,
@@ -45,7 +44,7 @@ export function useTheme(theme?: PartialEmotionCanvasTheme): EmotionCanvasTheme
45
44
  // Context not supported or invalid (probably called from within a class component)
46
45
  }
47
46
 
48
- const windowTheme = typeof window !== 'undefined' && get(window, 'window.workday.canvas.theme');
47
+ const windowTheme = typeof window !== 'undefined' && (window as any)?.workday?.canvas?.theme;
49
48
  if (windowTheme) {
50
49
  return getFilledTheme({canvas: windowTheme});
51
50
  }
@@ -0,0 +1,21 @@
1
+ const isObject = (obj: any): boolean => obj && typeof obj === 'object';
2
+ export function deepMerge<T, S>(target: T, source: S): T & S {
3
+ if (!isObject(target) || !isObject(source)) {
4
+ return source as T & S;
5
+ }
6
+
7
+ Object.keys(source).forEach(key => {
8
+ const targetValue = target[key as keyof T];
9
+ const sourceValue = source[key as keyof S];
10
+
11
+ if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
12
+ (target as any)[key] = targetValue.concat(sourceValue);
13
+ } else if (isObject(targetValue) && isObject(sourceValue)) {
14
+ (target as any)[key] = deepMerge(Object.assign({}, targetValue), sourceValue);
15
+ } else {
16
+ (target as any)[key] = sourceValue;
17
+ }
18
+ });
19
+
20
+ return target as T & S;
21
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Simple memoize function. It takes a function and a resolver function to generate a
3
+ * cache key
4
+ */
5
+ export function memoize<T extends (...args: any) => any>(
6
+ func: T,
7
+ resolver: (...args: Parameters<T>) => string
8
+ ): T {
9
+ const cache = new Map<string, ReturnType<T>>();
10
+
11
+ return function(...args: Parameters<T>): ReturnType<T> {
12
+ const key = resolver(...args);
13
+
14
+ if (cache.has(key)) {
15
+ return cache.get(key)!;
16
+ }
17
+
18
+ const result = func(...args);
19
+ cache.set(key, result);
20
+
21
+ return result;
22
+ } as T;
23
+ }
@@ -12,10 +12,7 @@ interface FocusRingOptions {
12
12
  inset?: 'inner' | 'outer';
13
13
  innerColor?: string;
14
14
  outerColor?: string;
15
- memoize?: boolean;
16
15
  }
17
- declare function calculateFocusRing({ width, separation, animate, inset, innerColor, outerColor, }: Required<Omit<FocusRingOptions, 'memoize' | 'inset'>> & Pick<FocusRingOptions, 'inset'>): CSSObject;
18
- export declare const memoizedFocusRing: typeof calculateFocusRing & import("lodash").MemoizedFunction;
19
16
  /**
20
17
  * A utility to create a canvas style focus ring around your widget.
21
18
  * By default, this mixin will create a 2px focus ring tightly wrapped
@@ -1 +1 @@
1
- {"version":3,"file":"focusRing.d.ts","sourceRoot":"","sources":["../../../../../common/lib/styles/focusRing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,EAAE,SAAS,EAAC,MAAM,gBAAgB,CAAC;AAK3D,UAAU,gBAAgB;IACxB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,iBAAS,kBAAkB,CAAC,EAC1B,KAAK,EACL,UAAU,EACV,OAAO,EACP,KAAK,EACL,UAAU,EACV,UAAU,GACX,EAAE,QAAQ,CAAC,IAAI,CAAC,gBAAgB,EAAE,SAAS,GAAG,OAAO,CAAC,CAAC,GACtD,IAAI,CAAC,gBAAgB,EAAE,OAAO,CAAC,GAAG,SAAS,CAiC5C;AAED,eAAO,MAAM,iBAAiB,+DAAiE,CAAC;AAEhG;;;;;;;;;;;;;;GAcG;AAEH;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,OAAO,GAAE,gBAAqB,EAAE,KAAK,CAAC,EAAE,KAAK,GAAG,SAAS,CA2BlF"}
1
+ {"version":3,"file":"focusRing.d.ts","sourceRoot":"","sources":["../../../../../common/lib/styles/focusRing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,EAAE,SAAS,EAAC,MAAM,gBAAgB,CAAC;AAI3D,UAAU,gBAAgB;IACxB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AA6CD;;;;;;;;;;;;;;GAcG;AAEH;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,OAAO,GAAE,gBAAqB,EAAE,KAAK,CAAC,EAAE,KAAK,GAAG,SAAS,CAsBlF"}
@@ -1,13 +1,9 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.focusRing = exports.memoizedFocusRing = void 0;
3
+ exports.focusRing = void 0;
7
4
  var react_1 = require("@emotion/react");
8
5
  var tokens_1 = require("@workday/canvas-kit-react/tokens");
9
6
  var index_1 = require("../theming/index");
10
- var memoize_1 = __importDefault(require("lodash/memoize"));
11
7
  function calculateFocusRing(_a) {
12
8
  var width = _a.width, separation = _a.separation, animate = _a.animate, inset = _a.inset, innerColor = _a.innerColor, outerColor = _a.outerColor;
13
9
  var boxShadow, innerWidth, outerWidth;
@@ -37,13 +33,6 @@ function calculateFocusRing(_a) {
37
33
  }
38
34
  return { boxShadow: boxShadow };
39
35
  }
40
- exports.memoizedFocusRing = memoize_1.default(calculateFocusRing, function () {
41
- var args = [];
42
- for (var _i = 0; _i < arguments.length; _i++) {
43
- args[_i] = arguments[_i];
44
- }
45
- return JSON.stringify(args);
46
- });
47
36
  /**
48
37
  * A utility to create a canvas style focus ring around your widget.
49
38
  * By default, this mixin will create a 2px focus ring tightly wrapped
@@ -70,7 +59,7 @@ function focusRing(options, theme) {
70
59
  if (options === void 0) { options = {}; }
71
60
  var _a = options.width, width = _a === void 0 ? 2 : _a, _b = options.separation, separation = _b === void 0 ? 0 : _b, _c = options.animate, animate = _c === void 0 ? true : _c, _d = options.innerColor, innerColor = _d === void 0 ? tokens_1.canvas.colors.frenchVanilla100 : _d, _e = options.outerColor, outerColor = _e === void 0 ? theme && theme.canvas
72
61
  ? theme.canvas.palette.common.focusOutline
73
- : index_1.defaultCanvasTheme.palette.common.focusOutline : _e, _f = options.memoize, memoize = _f === void 0 ? true : _f, inset = options.inset;
62
+ : index_1.defaultCanvasTheme.palette.common.focusOutline : _e, inset = options.inset;
74
63
  var args = {
75
64
  width: width,
76
65
  separation: separation,
@@ -79,9 +68,6 @@ function focusRing(options, theme) {
79
68
  animate: animate,
80
69
  inset: inset,
81
70
  };
82
- if (memoize) {
83
- return exports.memoizedFocusRing(args);
84
- }
85
71
  return calculateFocusRing(args);
86
72
  }
87
73
  exports.focusRing = focusRing;
@@ -1,10 +1,11 @@
1
1
  import { CanvasTheme, PartialCanvasTheme } from './types';
2
- export declare enum ColorDirection {
3
- Darken = 0,
4
- Brighten = 1
5
- }
6
- export declare function shiftColor(hexColor: string, direction: ColorDirection): string;
2
+ export declare const shiftColor: (hexColor: string, amount?: any) => string;
7
3
  declare function calculateCanvasTheme(partialTheme: PartialCanvasTheme): CanvasTheme;
8
- export declare const createCanvasTheme: typeof calculateCanvasTheme & import("lodash").MemoizedFunction;
4
+ /**
5
+ * Creates a full {@link CanvasTheme} from any partial theme by deeply merging with the
6
+ * `defaultCanvasTheme` object. The function is memoized, but it is best to run this function
7
+ * only once and save the result.
8
+ */
9
+ export declare const createCanvasTheme: typeof calculateCanvasTheme;
9
10
  export {};
10
11
  //# sourceMappingURL=createCanvasTheme.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createCanvasTheme.d.ts","sourceRoot":"","sources":["../../../../../common/lib/theming/createCanvasTheme.ts"],"names":[],"mappings":"AAMA,OAAO,EACL,WAAW,EACX,kBAAkB,EAInB,MAAM,SAAS,CAAC;AAIjB,oBAAY,cAAc;IACxB,MAAM,IAAA;IACN,QAAQ,IAAA;CACT;AAED,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,cAAc,UA4BrE;AAuCD,iBAAS,oBAAoB,CAAC,YAAY,EAAE,kBAAkB,GAAG,WAAW,CAkB3E;AAED,eAAO,MAAM,iBAAiB,iEAAmE,CAAC"}
1
+ {"version":3,"file":"createCanvasTheme.d.ts","sourceRoot":"","sources":["../../../../../common/lib/theming/createCanvasTheme.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,WAAW,EACX,kBAAkB,EAInB,MAAM,SAAS,CAAC;AAQjB,eAAO,MAAM,UAAU,aACV,MAAM,yBA+BlB,CAAC;AA+BF,iBAAS,oBAAoB,CAAC,YAAY,EAAE,kBAAkB,GAAG,WAAW,CAkB3E;AAED;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,6BAAmE,CAAC"}
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __rest = (this && this.__rest) || function (s, e) {
3
14
  var t = {};
4
15
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
@@ -14,62 +25,50 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
25
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
26
  };
16
27
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.createCanvasTheme = exports.shiftColor = exports.ColorDirection = void 0;
28
+ exports.createCanvasTheme = exports.shiftColor = void 0;
18
29
  var chroma_js_1 = __importDefault(require("chroma-js"));
19
- var merge_1 = __importDefault(require("lodash/merge"));
20
- var memoize_1 = __importDefault(require("lodash/memoize"));
21
- var findKey_1 = __importDefault(require("lodash/findKey"));
22
30
  var canvas_colors_web_1 = __importDefault(require("@workday/canvas-colors-web"));
23
31
  var theme_1 = require("./theme");
24
32
  var types_1 = require("./types");
25
33
  var utils_1 = require("../utils");
26
- var ColorDirection;
27
- (function (ColorDirection) {
28
- ColorDirection[ColorDirection["Darken"] = 0] = "Darken";
29
- ColorDirection[ColorDirection["Brighten"] = 1] = "Brighten";
30
- })(ColorDirection = exports.ColorDirection || (exports.ColorDirection = {}));
31
- function shiftColor(hexColor, direction) {
32
- var canvasColor = findKey_1.default(canvas_colors_web_1.default, function (color) { return color === hexColor; });
33
- var darken = direction === ColorDirection.Darken;
34
+ // Should these be exported?
35
+ var deepMerge_1 = require("../utils/deepMerge");
36
+ var memoize_1 = require("../utils/memoize");
37
+ exports.shiftColor = memoize_1.memoize(function (hexColor, amount) {
38
+ if (amount === void 0) { amount = 100; }
39
+ var canvasColor = Object.keys(canvas_colors_web_1.default).find(function (color) { return canvas_colors_web_1.default[color] === hexColor; });
34
40
  if (canvasColor) {
35
41
  var colorRegex = /([a-zAz]*)(\d{3})/g;
36
42
  var match = colorRegex.exec(canvasColor);
37
43
  if (match) {
38
44
  var baseColor = match[1];
39
45
  var shadeNumber = parseInt(match[2], 10);
40
- var newShade = darken ? shadeNumber + 100 : shadeNumber - 100;
46
+ var newShade = shadeNumber + amount;
41
47
  if (newShade >= 100 && newShade <= 600) {
42
48
  return canvas_colors_web_1.default[(baseColor + newShade)];
43
49
  }
44
50
  }
45
51
  }
46
52
  try {
47
- var newColor = darken ? chroma_js_1.default(hexColor).darken() : chroma_js_1.default(hexColor).brighten();
53
+ var newColor = amount > 0
54
+ ? chroma_js_1.default(hexColor).darken(amount / 100)
55
+ : chroma_js_1.default(hexColor).brighten(-amount / 100);
48
56
  return newColor.hex();
49
57
  }
50
58
  catch (e) {
51
59
  console.warn("Invalid color '" + hexColor + "' used in theme");
52
60
  return hexColor;
53
61
  }
54
- }
55
- exports.shiftColor = shiftColor;
62
+ }, function (color, value) { return color + "." + value; });
56
63
  function fillPalette(defaultPalette, palette) {
57
64
  if (!palette) {
58
65
  return {};
59
66
  }
60
67
  var main = palette.main || defaultPalette.main;
61
- var dark = palette.dark ||
62
- (palette.main && shiftColor(palette.main, ColorDirection.Darken)) ||
63
- defaultPalette.dark;
64
- var darkest = palette.darkest ||
65
- (palette.main && shiftColor(dark, ColorDirection.Darken)) ||
66
- defaultPalette.darkest;
67
- var light = palette.light ||
68
- (palette.main && shiftColor(palette.main, ColorDirection.Brighten)) ||
69
- defaultPalette.light;
70
- var lightest = palette.lightest ||
71
- (palette.main && shiftColor(light, ColorDirection.Brighten)) ||
72
- defaultPalette.lightest;
68
+ var dark = palette.dark || (palette.main && exports.shiftColor(palette.main, 100)) || defaultPalette.dark;
69
+ var darkest = palette.darkest || (palette.main && exports.shiftColor(palette.main, 200)) || defaultPalette.darkest;
70
+ var light = palette.light || (palette.main && exports.shiftColor(palette.main, -100)) || defaultPalette.light;
71
+ var lightest = palette.lightest || (palette.main && exports.shiftColor(palette.main, -200)) || defaultPalette.lightest;
73
72
  var contrast = palette.contrast || utils_1.pickForegroundColor(main) || defaultPalette.contrast;
74
73
  return {
75
74
  lightest: lightest,
@@ -95,9 +94,14 @@ function calculateCanvasTheme(partialTheme) {
95
94
  breakpoints: breakpoints,
96
95
  direction: direction === types_1.ContentDirection.RTL ? direction : types_1.ContentDirection.LTR,
97
96
  };
98
- return merge_1.default({}, theme_1.defaultCanvasTheme, mergeable, extraFields);
97
+ return deepMerge_1.deepMerge(deepMerge_1.deepMerge({}, theme_1.defaultCanvasTheme), __assign(__assign({}, mergeable), extraFields));
99
98
  }
100
- exports.createCanvasTheme = memoize_1.default(calculateCanvasTheme, function () {
99
+ /**
100
+ * Creates a full {@link CanvasTheme} from any partial theme by deeply merging with the
101
+ * `defaultCanvasTheme` object. The function is memoized, but it is best to run this function
102
+ * only once and save the result.
103
+ */
104
+ exports.createCanvasTheme = memoize_1.memoize(calculateCanvasTheme, function () {
101
105
  var args = [];
102
106
  for (var _i = 0; _i < arguments.length; _i++) {
103
107
  args[_i] = arguments[_i];
@@ -1,9 +1,15 @@
1
+ /**
2
+ * For custom themes that do not overwrite every default.
3
+ */
4
+ export declare type RecursivePartial<T> = {
5
+ [P in keyof T]?: RecursivePartial<T[P]>;
6
+ };
1
7
  /**
2
8
  * The functions gets a new object proxy. This is used for theme to ensure regardless of a partial theme object
3
9
  * there will be a fallback theme object so that properties aren't undefined regardless of the `target` object.
4
10
  * This function will also handle nested properties.
5
- * @param target Any custom theme object
6
- * @param fallback A fallback theme object
11
+ * @param target A partial object with up to the same shape as the `fallback` object
12
+ * @param fallback A fallback object. If a property to be accessed from the proxy is not available on the target object, the fallback object will be used
7
13
  */
8
- export declare function getObjectProxy<T extends {}>(target: unknown, fallback: T): T;
14
+ export declare function getObjectProxy<T extends {}>(target: RecursivePartial<T> | undefined, fallback: T): T;
9
15
  //# sourceMappingURL=getObjectProxy.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getObjectProxy.d.ts","sourceRoot":"","sources":["../../../../../common/lib/theming/getObjectProxy.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,wBAAgB,cAAc,CAAC,CAAC,SAAS,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,CAgB5E"}
1
+ {"version":3,"file":"getObjectProxy.d.ts","sourceRoot":"","sources":["../../../../../common/lib/theming/getObjectProxy.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,gBAAgB,CAAC,CAAC,IAAI;KAC/B,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACxC,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,EAAE,EACzC,MAAM,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,SAAS,EACvC,QAAQ,EAAE,CAAC,GACV,CAAC,CAmBH"}
@@ -5,27 +5,27 @@ exports.getObjectProxy = void 0;
5
5
  * The functions gets a new object proxy. This is used for theme to ensure regardless of a partial theme object
6
6
  * there will be a fallback theme object so that properties aren't undefined regardless of the `target` object.
7
7
  * This function will also handle nested properties.
8
- * @param target Any custom theme object
9
- * @param fallback A fallback theme object
8
+ * @param target A partial object with up to the same shape as the `fallback` object
9
+ * @param fallback A fallback object. If a property to be accessed from the proxy is not available on the target object, the fallback object will be used
10
10
  */
11
- /* eslint-disable compat/compat */
12
11
  function getObjectProxy(target, fallback) {
13
- return new Proxy(target, {
14
- get: function (target, p) {
15
- var prop = p; // keep Typescript happy - the `get` signature cannot contain `keyof T`
16
- if (typeof fallback[prop] === 'object' && typeof target[prop] === 'object') {
17
- return getObjectProxy(target[prop] || {}, fallback[prop]);
18
- }
19
- else if (target[prop] !== undefined) {
20
- return target[prop];
21
- }
22
- else {
23
- return fallback[prop];
24
- }
25
- },
26
- ownKeys: function (target) {
27
- return Object.keys(fallback);
28
- },
29
- });
12
+ if (target) {
13
+ return new Proxy(target, {
14
+ get: function (target, prop, receiver) {
15
+ if (typeof fallback[prop] === 'object') {
16
+ return getObjectProxy(target[prop], fallback[prop]);
17
+ }
18
+ var targetProp = target[prop];
19
+ if (targetProp !== undefined) {
20
+ return targetProp;
21
+ }
22
+ return Reflect.get(fallback, prop, receiver);
23
+ },
24
+ ownKeys: function () {
25
+ return Reflect.ownKeys(fallback);
26
+ },
27
+ });
28
+ }
29
+ return fallback;
30
30
  }
31
31
  exports.getObjectProxy = getObjectProxy;
@@ -1 +1 @@
1
- {"version":3,"file":"useTheme.d.ts","sourceRoot":"","sources":["../../../../../common/lib/theming/useTheme.ts"],"names":[],"mappings":"AAEA,OAAO,EAGL,kBAAkB,EAClB,yBAAyB,EAC1B,MAAM,SAAS,CAAC;AAOjB;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CAAC,KAAK,CAAC,EAAE,yBAAyB,GAAG,kBAAkB,CAqB9E"}
1
+ {"version":3,"file":"useTheme.d.ts","sourceRoot":"","sources":["../../../../../common/lib/theming/useTheme.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,kBAAkB,EAClB,yBAAyB,EAC1B,MAAM,SAAS,CAAC;AAOjB;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CAAC,KAAK,CAAC,EAAE,yBAAyB,GAAG,kBAAkB,CAqB9E"}
@@ -10,12 +10,8 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
13
  Object.defineProperty(exports, "__esModule", { value: true });
17
14
  exports.useTheme = void 0;
18
- var get_1 = __importDefault(require("lodash/get"));
19
15
  var react_1 = require("@emotion/react");
20
16
  var index_1 = require("./index");
21
17
  var getFilledTheme = function (theme) { return (__assign(__assign({}, theme), { canvas: index_1.createCanvasTheme(theme.canvas) })); };
@@ -38,6 +34,7 @@ var getFilledTheme = function (theme) { return (__assign(__assign({}, theme), {
38
34
  * Tracked on https://github.com/emotion-js/emotion/issues/1193.
39
35
  */
40
36
  function useTheme(theme) {
37
+ var _a, _b, _c;
41
38
  if (theme && theme.canvas) {
42
39
  return getFilledTheme(theme);
43
40
  }
@@ -51,7 +48,7 @@ function useTheme(theme) {
51
48
  catch (e) {
52
49
  // Context not supported or invalid (probably called from within a class component)
53
50
  }
54
- var windowTheme = typeof window !== 'undefined' && get_1.default(window, 'window.workday.canvas.theme');
51
+ var windowTheme = typeof window !== 'undefined' && ((_c = (_b = (_a = window) === null || _a === void 0 ? void 0 : _a.workday) === null || _b === void 0 ? void 0 : _b.canvas) === null || _c === void 0 ? void 0 : _c.theme);
55
52
  if (windowTheme) {
56
53
  return getFilledTheme({ canvas: windowTheme });
57
54
  }
@@ -0,0 +1,2 @@
1
+ export declare function deepMerge<T, S>(target: T, source: S): T & S;
2
+ //# sourceMappingURL=deepMerge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deepMerge.d.ts","sourceRoot":"","sources":["../../../../../common/lib/utils/deepMerge.ts"],"names":[],"mappings":"AACA,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAmB3D"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deepMerge = void 0;
4
+ var isObject = function (obj) { return obj && typeof obj === 'object'; };
5
+ function deepMerge(target, source) {
6
+ if (!isObject(target) || !isObject(source)) {
7
+ return source;
8
+ }
9
+ Object.keys(source).forEach(function (key) {
10
+ var targetValue = target[key];
11
+ var sourceValue = source[key];
12
+ if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
13
+ target[key] = targetValue.concat(sourceValue);
14
+ }
15
+ else if (isObject(targetValue) && isObject(sourceValue)) {
16
+ target[key] = deepMerge(Object.assign({}, targetValue), sourceValue);
17
+ }
18
+ else {
19
+ target[key] = sourceValue;
20
+ }
21
+ });
22
+ return target;
23
+ }
24
+ exports.deepMerge = deepMerge;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Simple memoize function. It takes a function and a resolver function to generate a
3
+ * cache key
4
+ */
5
+ export declare function memoize<T extends (...args: any) => any>(func: T, resolver: (...args: Parameters<T>) => string): T;
6
+ //# sourceMappingURL=memoize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memoize.d.ts","sourceRoot":"","sources":["../../../../../common/lib/utils/memoize.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,KAAK,GAAG,EACrD,IAAI,EAAE,CAAC,EACP,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,MAAM,GAC3C,CAAC,CAeH"}