@fluentui-react-native/use-tokens 0.1.6
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.
- package/CHANGELOG.json +200 -0
- package/CHANGELOG.md +53 -0
- package/README.md +85 -0
- package/babel.config.js +1 -0
- package/just.config.js +3 -0
- package/lib/applyPropsToTokens.d.ts +3 -0
- package/lib/applyPropsToTokens.d.ts.map +1 -0
- package/lib/applyPropsToTokens.js +28 -0
- package/lib/applyPropsToTokens.js.map +1 -0
- package/lib/applyPropsToTokens.test.d.ts +2 -0
- package/lib/applyPropsToTokens.test.d.ts.map +1 -0
- package/lib/applyPropsToTokens.test.js +30 -0
- package/lib/applyPropsToTokens.test.js.map +1 -0
- package/lib/applyTokenLayers.d.ts +16 -0
- package/lib/applyTokenLayers.d.ts.map +1 -0
- package/lib/applyTokenLayers.js +25 -0
- package/lib/applyTokenLayers.js.map +1 -0
- package/lib/applyTokenLayers.test.d.ts +2 -0
- package/lib/applyTokenLayers.test.d.ts.map +1 -0
- package/lib/applyTokenLayers.test.js +64 -0
- package/lib/applyTokenLayers.test.js.map +1 -0
- package/lib/buildUseTokens.d.ts +35 -0
- package/lib/buildUseTokens.d.ts.map +1 -0
- package/lib/buildUseTokens.js +62 -0
- package/lib/buildUseTokens.js.map +1 -0
- package/lib/buildUseTokens.test.d.ts +2 -0
- package/lib/buildUseTokens.test.d.ts.map +1 -0
- package/lib/buildUseTokens.test.js +103 -0
- package/lib/buildUseTokens.test.js.map +1 -0
- package/lib/customizable.d.ts +24 -0
- package/lib/customizable.d.ts.map +1 -0
- package/lib/customizable.js +22 -0
- package/lib/customizable.js.map +1 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +6 -0
- package/lib/index.js.map +1 -0
- package/lib/patchTokens.d.ts +12 -0
- package/lib/patchTokens.d.ts.map +1 -0
- package/lib/patchTokens.js +42 -0
- package/lib/patchTokens.js.map +1 -0
- package/lib/patchTokens.test.d.ts +2 -0
- package/lib/patchTokens.test.d.ts.map +1 -0
- package/lib/patchTokens.test.js +38 -0
- package/lib/patchTokens.test.js.map +1 -0
- package/lib/useTokens.samples.test.d.ts +2 -0
- package/lib/useTokens.samples.test.d.ts.map +1 -0
- package/lib/useTokens.samples.test.js +165 -0
- package/lib/useTokens.samples.test.js.map +1 -0
- package/lib-commonjs/applyPropsToTokens.d.ts +3 -0
- package/lib-commonjs/applyPropsToTokens.d.ts.map +1 -0
- package/lib-commonjs/applyPropsToTokens.js +31 -0
- package/lib-commonjs/applyPropsToTokens.js.map +1 -0
- package/lib-commonjs/applyPropsToTokens.test.d.ts +2 -0
- package/lib-commonjs/applyPropsToTokens.test.d.ts.map +1 -0
- package/lib-commonjs/applyPropsToTokens.test.js +32 -0
- package/lib-commonjs/applyPropsToTokens.test.js.map +1 -0
- package/lib-commonjs/applyTokenLayers.d.ts +16 -0
- package/lib-commonjs/applyTokenLayers.d.ts.map +1 -0
- package/lib-commonjs/applyTokenLayers.js +28 -0
- package/lib-commonjs/applyTokenLayers.js.map +1 -0
- package/lib-commonjs/applyTokenLayers.test.d.ts +2 -0
- package/lib-commonjs/applyTokenLayers.test.d.ts.map +1 -0
- package/lib-commonjs/applyTokenLayers.test.js +66 -0
- package/lib-commonjs/applyTokenLayers.test.js.map +1 -0
- package/lib-commonjs/buildUseTokens.d.ts +35 -0
- package/lib-commonjs/buildUseTokens.d.ts.map +1 -0
- package/lib-commonjs/buildUseTokens.js +65 -0
- package/lib-commonjs/buildUseTokens.js.map +1 -0
- package/lib-commonjs/buildUseTokens.test.d.ts +2 -0
- package/lib-commonjs/buildUseTokens.test.d.ts.map +1 -0
- package/lib-commonjs/buildUseTokens.test.js +105 -0
- package/lib-commonjs/buildUseTokens.test.js.map +1 -0
- package/lib-commonjs/customizable.d.ts +24 -0
- package/lib-commonjs/customizable.d.ts.map +1 -0
- package/lib-commonjs/customizable.js +25 -0
- package/lib-commonjs/customizable.js.map +1 -0
- package/lib-commonjs/index.d.ts +6 -0
- package/lib-commonjs/index.d.ts.map +1 -0
- package/lib-commonjs/index.js +11 -0
- package/lib-commonjs/index.js.map +1 -0
- package/lib-commonjs/patchTokens.d.ts +12 -0
- package/lib-commonjs/patchTokens.d.ts.map +1 -0
- package/lib-commonjs/patchTokens.js +45 -0
- package/lib-commonjs/patchTokens.js.map +1 -0
- package/lib-commonjs/patchTokens.test.d.ts +2 -0
- package/lib-commonjs/patchTokens.test.d.ts.map +1 -0
- package/lib-commonjs/patchTokens.test.js +40 -0
- package/lib-commonjs/patchTokens.test.js.map +1 -0
- package/lib-commonjs/useTokens.samples.test.d.ts +2 -0
- package/lib-commonjs/useTokens.samples.test.d.ts.map +1 -0
- package/lib-commonjs/useTokens.samples.test.js +177 -0
- package/lib-commonjs/useTokens.samples.test.js.map +1 -0
- package/package.json +41 -0
- package/src/__snapshots__/useTokens.samples.test.tsx.snap +163 -0
- package/src/applyPropsToTokens.test.ts +47 -0
- package/src/applyPropsToTokens.ts +15 -0
- package/src/applyTokenLayers.test.ts +67 -0
- package/src/applyTokenLayers.ts +40 -0
- package/src/buildUseTokens.test.ts +113 -0
- package/src/buildUseTokens.ts +89 -0
- package/src/customizable.ts +36 -0
- package/src/index.ts +5 -0
- package/src/patchTokens.test.ts +50 -0
- package/src/patchTokens.ts +30 -0
- package/src/useTokens.samples.test.tsx +219 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
2
|
+
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
|
3
|
+
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
|
4
|
+
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
|
5
|
+
r[k] = a[j];
|
|
6
|
+
return r;
|
|
7
|
+
};
|
|
8
|
+
import { getMemoCache } from '@fluentui-react-native/memo-cache';
|
|
9
|
+
import { immutableMerge } from '@fluentui-react-native/immutable-merge';
|
|
10
|
+
/**
|
|
11
|
+
* Tokens are defined as either:
|
|
12
|
+
* TTokens - an object
|
|
13
|
+
* string - a name to look up in the theme
|
|
14
|
+
* function - a function to run against the theme to produce tokens
|
|
15
|
+
*
|
|
16
|
+
* This function maps any of these types into a specific TTokens object. A string is first lookup up in the theme, returning a function
|
|
17
|
+
* or object. If the type is a function this will be invoked with the theme to generate the tokens object.
|
|
18
|
+
*
|
|
19
|
+
* @param tokenEntry - token entry to start with
|
|
20
|
+
* @param theme - theme to use for queries
|
|
21
|
+
* @param getComponentInfo - helper to use to lookup the component in the theme
|
|
22
|
+
*/
|
|
23
|
+
function mapToTokens(tokenEntry, theme, getComponentInfo) {
|
|
24
|
+
if (typeof tokenEntry === 'string') {
|
|
25
|
+
tokenEntry = (getComponentInfo && getComponentInfo(theme, tokenEntry)) || {};
|
|
26
|
+
}
|
|
27
|
+
if (typeof tokenEntry === 'function') {
|
|
28
|
+
tokenEntry = tokenEntry(theme);
|
|
29
|
+
}
|
|
30
|
+
return tokenEntry;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Construct a useStyling hook which returns styled slot props based on props and tokens defined in options and in the theme
|
|
34
|
+
*
|
|
35
|
+
* @param options - options which drive behavior for the generated styling hook
|
|
36
|
+
* @param themeHelper - injected theme functionality
|
|
37
|
+
*/
|
|
38
|
+
export function buildUseTokens(getComponentInfo) {
|
|
39
|
+
var tokens = [];
|
|
40
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
41
|
+
tokens[_i - 1] = arguments[_i];
|
|
42
|
+
}
|
|
43
|
+
// create a cache instance for use in this particular call to buildUseTokens
|
|
44
|
+
var cache = getMemoCache();
|
|
45
|
+
// the core function simply merges layers together, looking up component definitions in the theme as well as executing any
|
|
46
|
+
// theme functions. This turns the tokens into an array of token objects that then get merged together
|
|
47
|
+
var useTokensCore = function (theme) {
|
|
48
|
+
// get the base styles all merged together, these will only depend on internal tokens and theme
|
|
49
|
+
return cache(function () { return immutableMerge.apply(void 0, tokens.map(function (value) { return mapToTokens(value, theme, getComponentInfo); })); }, [theme]);
|
|
50
|
+
};
|
|
51
|
+
// attach a customize function to generate a new use
|
|
52
|
+
useTokensCore.customize = function () {
|
|
53
|
+
var newTokens = [];
|
|
54
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
55
|
+
newTokens[_i] = arguments[_i];
|
|
56
|
+
}
|
|
57
|
+
var mergedTokens = __spreadArrays(tokens, newTokens);
|
|
58
|
+
return buildUseTokens.apply(void 0, __spreadArrays([getComponentInfo], mergedTokens));
|
|
59
|
+
};
|
|
60
|
+
return useTokensCore;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=buildUseTokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildUseTokens.js","sourceRoot":"","sources":["../src/buildUseTokens.ts"],"names":[],"mappings":";;;;;;;AAAA,OAAO,EAAE,YAAY,EAAgB,MAAM,mCAAmC,CAAC;AAC/E,OAAO,EAAE,cAAc,EAAE,MAAM,wCAAwC,CAAC;AAiCxE;;;;;;;;;;;;GAYG;AACH,SAAS,WAAW,CAClB,UAA+D,EAC/D,KAAa,EACb,gBAA+D;IAE/D,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE;QAClC,UAAU,GAAG,CAAC,gBAAgB,IAAK,gBAAgB,CAAC,KAAK,EAAE,UAAU,CAAa,CAAC,IAAK,EAAc,CAAC;KACxG;IACD,IAAI,OAAO,UAAU,KAAK,UAAU,EAAE;QACpC,UAAU,GAAI,UAA+C,CAAC,KAAK,CAAC,CAAC;KACtE;IACD,OAAO,UAA+B,CAAC;AACzC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAC5B,gBAA+D;IAC/D,gBAA2C;SAA3C,UAA2C,EAA3C,qBAA2C,EAA3C,IAA2C;QAA3C,+BAA2C;;IAE3C,4EAA4E;IAC5E,IAAM,KAAK,GAAG,YAAY,EAAE,CAAC;IAE7B,0HAA0H;IAC1H,sGAAsG;IACtG,IAAM,aAAa,GAAG,UAAC,KAAa;QAClC,+FAA+F;QAC/F,OAAO,KAAK,CAAC,cAAM,OAAA,cAAc,eAAI,MAAM,CAAC,GAAG,CAAC,UAAC,KAAK,IAAK,OAAA,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,gBAAgB,CAAC,EAA3C,CAA2C,CAAC,GAApF,CAAqF,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IACrH,CAAC,CAAC;IAEF,oDAAoD;IACpD,aAAa,CAAC,SAAS,GAAG;QAAC,mBAA8C;aAA9C,UAA8C,EAA9C,qBAA8C,EAA9C,IAA8C;YAA9C,8BAA8C;;QACvE,IAAM,YAAY,kBAAO,MAAM,EAAK,SAAS,CAAC,CAAC;QAC/C,OAAO,cAAc,+BAAkB,gBAAgB,GAAK,YAAY,GAAE;IAC5E,CAAC,CAAC;IAEF,OAAO,aAAa,CAAC;AACvB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildUseTokens.test.d.ts","sourceRoot":"","sources":["../src/buildUseTokens.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
13
|
+
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
|
14
|
+
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
|
15
|
+
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
|
16
|
+
r[k] = a[j];
|
|
17
|
+
return r;
|
|
18
|
+
};
|
|
19
|
+
import { buildUseTokens } from './buildUseTokens';
|
|
20
|
+
var baseTokens = {
|
|
21
|
+
a: 'a-base',
|
|
22
|
+
b: 'b-base',
|
|
23
|
+
c: 'c-base',
|
|
24
|
+
hover: {
|
|
25
|
+
c: 'c-base-hover',
|
|
26
|
+
},
|
|
27
|
+
press: {
|
|
28
|
+
c: 'c-base-press',
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
var defaultTheme = {
|
|
32
|
+
vals: {
|
|
33
|
+
foo: 'foo',
|
|
34
|
+
bar: 'bar',
|
|
35
|
+
},
|
|
36
|
+
components: {
|
|
37
|
+
uno: {
|
|
38
|
+
a: 'uno-a',
|
|
39
|
+
c: 'uno-c',
|
|
40
|
+
},
|
|
41
|
+
dos: {
|
|
42
|
+
b: 'dos-b',
|
|
43
|
+
c: 'dos-c',
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
var variantTheme = {
|
|
48
|
+
vals: {
|
|
49
|
+
foo: 'variant',
|
|
50
|
+
},
|
|
51
|
+
components: {},
|
|
52
|
+
};
|
|
53
|
+
var getComponentInfo = function (theme, name) { return theme.components[name]; };
|
|
54
|
+
var componentTokens = [baseTokens, 'uno', function (theme) { return ({ b: theme.vals.foo }); }];
|
|
55
|
+
var resolvedTokens = {
|
|
56
|
+
a: 'uno-a',
|
|
57
|
+
b: 'foo',
|
|
58
|
+
c: 'uno-c',
|
|
59
|
+
hover: {
|
|
60
|
+
c: 'c-base-hover',
|
|
61
|
+
},
|
|
62
|
+
press: {
|
|
63
|
+
c: 'c-base-press',
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
var variantTokens = {
|
|
67
|
+
a: 'a-base',
|
|
68
|
+
b: 'variant',
|
|
69
|
+
c: 'c-base',
|
|
70
|
+
hover: {
|
|
71
|
+
c: 'c-base-hover',
|
|
72
|
+
},
|
|
73
|
+
press: {
|
|
74
|
+
c: 'c-base-press',
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
describe('buildUseTokens test suite', function () {
|
|
78
|
+
test('basic built hook', function () {
|
|
79
|
+
var useTokens = buildUseTokens.apply(void 0, __spreadArrays([getComponentInfo], componentTokens));
|
|
80
|
+
var tokens = useTokens(defaultTheme)[0];
|
|
81
|
+
expect(tokens).toEqual(resolvedTokens);
|
|
82
|
+
});
|
|
83
|
+
test('multiple calls return same object', function () {
|
|
84
|
+
var useTokens = buildUseTokens.apply(void 0, __spreadArrays([getComponentInfo], componentTokens));
|
|
85
|
+
var tokens1 = useTokens(defaultTheme)[0];
|
|
86
|
+
var tokens2 = useTokens(defaultTheme)[0];
|
|
87
|
+
expect(tokens1).toBe(tokens2);
|
|
88
|
+
});
|
|
89
|
+
test('variant theme is separate', function () {
|
|
90
|
+
var useTokens = buildUseTokens.apply(void 0, __spreadArrays([getComponentInfo], componentTokens));
|
|
91
|
+
var tokensDefault = useTokens(defaultTheme)[0];
|
|
92
|
+
var tokensVariant = useTokens(variantTheme)[0];
|
|
93
|
+
expect(tokensVariant).not.toBe(tokensDefault);
|
|
94
|
+
expect(tokensVariant).toEqual(variantTokens);
|
|
95
|
+
});
|
|
96
|
+
test('simple customization layers on top', function () {
|
|
97
|
+
var useTokens = buildUseTokens.apply(void 0, __spreadArrays([getComponentInfo], componentTokens));
|
|
98
|
+
var useTokensCustom = useTokens.customize({ a: 'custom' });
|
|
99
|
+
var tokens = useTokensCustom(defaultTheme)[0];
|
|
100
|
+
expect(tokens).toEqual(__assign(__assign({}, resolvedTokens), { a: 'custom' }));
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
//# sourceMappingURL=buildUseTokens.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildUseTokens.test.js","sourceRoot":"","sources":["../src/buildUseTokens.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,cAAc,EAAE,MA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"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { TokenSettings, UseTokens } from './buildUseTokens';
|
|
3
|
+
/**
|
|
4
|
+
* A component implementation, with a use tokens hook passed in. Implementing it this way allows the useTokens hook to be
|
|
5
|
+
* modified by the customization handler
|
|
6
|
+
*/
|
|
7
|
+
export declare type InjectableComponent<TProps, TTokens, TTheme> = (props: TProps, useTokens: UseTokens<TTokens, TTheme>) => JSX.Element | null;
|
|
8
|
+
/**
|
|
9
|
+
* A component with an attached customize function, used to create alternatively styled versions of the component
|
|
10
|
+
*/
|
|
11
|
+
export declare type CustomizableComponent<TProps, TTokens, TTheme> = React.FunctionComponent<TProps> & {
|
|
12
|
+
customize: (...tokens: TokenSettings<TTokens, TTheme>[]) => CustomizableComponent<TProps, TTokens, TTheme>;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Function helper for easily creating a customizable component based on the useTokens hook
|
|
16
|
+
*
|
|
17
|
+
* @param injectable - a function component implementation, written in (props, useTokens) => JSX.Element form
|
|
18
|
+
* @param useTokens - a hook function, generally built via buildUseTokens, used to retrieve design tokens for the component
|
|
19
|
+
*
|
|
20
|
+
* @returns - a function component that has a static function called customize attached. Customize will return a
|
|
21
|
+
* new component (which can also be customized)
|
|
22
|
+
*/
|
|
23
|
+
export declare function customizable<TProps, TTokens, TTheme>(injectable: InjectableComponent<TProps, TTokens, TTheme>, useTokens: UseTokens<TTokens, TTheme>): CustomizableComponent<TProps, TTokens, TTheme>;
|
|
24
|
+
//# sourceMappingURL=customizable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customizable.d.ts","sourceRoot":"","sources":["../src/customizable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE5D;;;GAGG;AACH,oBAAY,mBAAmB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,KAAK,GAAG,CAAC,OAAO,GAAG,IAAI,CAAC;AAExI;;GAEG;AACH,oBAAY,qBAAqB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,MAAM,CAAC,GAAG;IAC7F,SAAS,EAAE,CAAC,GAAG,MAAM,EAAE,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,KAAK,qBAAqB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;CAC5G,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAClD,UAAU,EAAE,mBAAmB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,EACxD,SAAS,EAAE,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,GACpC,qBAAqB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAOhD"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function helper for easily creating a customizable component based on the useTokens hook
|
|
3
|
+
*
|
|
4
|
+
* @param injectable - a function component implementation, written in (props, useTokens) => JSX.Element form
|
|
5
|
+
* @param useTokens - a hook function, generally built via buildUseTokens, used to retrieve design tokens for the component
|
|
6
|
+
*
|
|
7
|
+
* @returns - a function component that has a static function called customize attached. Customize will return a
|
|
8
|
+
* new component (which can also be customized)
|
|
9
|
+
*/
|
|
10
|
+
export function customizable(injectable, useTokens) {
|
|
11
|
+
var component = function (props) { return injectable(props, useTokens); };
|
|
12
|
+
component.customize = function () {
|
|
13
|
+
var tokens = [];
|
|
14
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
15
|
+
tokens[_i] = arguments[_i];
|
|
16
|
+
}
|
|
17
|
+
var useTokensNew = useTokens.customize.apply(useTokens, tokens);
|
|
18
|
+
return customizable(injectable, useTokensNew);
|
|
19
|
+
};
|
|
20
|
+
return component;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=customizable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customizable.js","sourceRoot":"","sources":["../src/customizable.ts"],"names":[],"mappings":"AAgBA;;;;;;;;GAQG;AACH,MAAM,UAAU,YAAY,CAC1B,UAAwD,EACxD,SAAqC;IAErC,IAAM,SAAS,GAAG,UAAC,KAAa,IAAK,OAAA,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,EAA5B,CAA4B,CAAC;IAClE,SAAS,CAAC,SAAS,GAAG;QAAC,gBAA2C;aAA3C,UAA2C,EAA3C,qBAA2C,EAA3C,IAA2C;YAA3C,2BAA2C;;QAChE,IAAM,YAAY,GAAG,SAAS,CAAC,SAAS,OAAnB,SAAS,EAAc,MAAM,CAAC,CAAC;QACpD,OAAO,YAAY,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;IAChD,CAAC,CAAC;IACF,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC"}
|
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { GetMemoValue } from '@fluentui-react-native/memo-cache';
|
|
2
|
+
/**
|
|
3
|
+
* Take a set of tokens (and a memo-cache) and apply changes to those tokens from an additional set of tokens. Only keys which are
|
|
4
|
+
* not undefined will be applied and if no changes are detected the token object will be unchanged.
|
|
5
|
+
*
|
|
6
|
+
* @param tokens - base set of tokens to apply changes to, this will not be modified
|
|
7
|
+
* @param cache - cache corresponding to this set of tokens
|
|
8
|
+
* @param patchValues - new values to apply, values will be obtained via keys in the object
|
|
9
|
+
* @returns - a tuple consisting of a new tokens object and a new memo-cache
|
|
10
|
+
*/
|
|
11
|
+
export declare function patchTokens<TTokens>(tokens: TTokens, cache: GetMemoValue<TTokens>, patchValues: TTokens): [TTokens, GetMemoValue<TTokens>];
|
|
12
|
+
//# sourceMappingURL=patchTokens.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"patchTokens.d.ts","sourceRoot":"","sources":["../src/patchTokens.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAEjE;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,OAAO,EACjC,MAAM,EAAE,OAAO,EACf,KAAK,EAAE,YAAY,CAAC,OAAO,CAAC,EAC5B,WAAW,EAAE,OAAO,GACnB,CAAC,OAAO,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,CAclC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Take a set of tokens (and a memo-cache) and apply changes to those tokens from an additional set of tokens. Only keys which are
|
|
14
|
+
* not undefined will be applied and if no changes are detected the token object will be unchanged.
|
|
15
|
+
*
|
|
16
|
+
* @param tokens - base set of tokens to apply changes to, this will not be modified
|
|
17
|
+
* @param cache - cache corresponding to this set of tokens
|
|
18
|
+
* @param patchValues - new values to apply, values will be obtained via keys in the object
|
|
19
|
+
* @returns - a tuple consisting of a new tokens object and a new memo-cache
|
|
20
|
+
*/
|
|
21
|
+
export function patchTokens(tokens, cache, patchValues) {
|
|
22
|
+
// reduce the patch values to the set of keys that are defined, and sort them to ensure consistent ordering
|
|
23
|
+
var keys = Object.keys(patchValues)
|
|
24
|
+
.filter(function (v) { return patchValues[v] !== undefined; })
|
|
25
|
+
.sort();
|
|
26
|
+
var _loop_1 = function (key) {
|
|
27
|
+
var _a;
|
|
28
|
+
_a = cache(function () {
|
|
29
|
+
var _a;
|
|
30
|
+
return (__assign(__assign({}, tokens), (_a = {}, _a[key] = patchValues[key], _a)));
|
|
31
|
+
}, [key, patchValues[key]]), tokens = _a[0], cache = _a[1];
|
|
32
|
+
};
|
|
33
|
+
// for each key get an updated tokens collection based on key + value. Value alone isn't sufficient as the values
|
|
34
|
+
// are not necessarily unique. i.e. { a: 'blue' } and { b: 'blue' } would cache to the same without the key
|
|
35
|
+
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
36
|
+
var key = keys_1[_i];
|
|
37
|
+
_loop_1(key);
|
|
38
|
+
}
|
|
39
|
+
// return the updated tokens and cache (if there were any keys applied)
|
|
40
|
+
return [tokens, cache];
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=patchTokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"patchTokens.js","sourceRoot":"","sources":["../src/patchTokens.ts"],"names":[],"mappings":";;;;;;;;;;;AAEA;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CACzB,MAAe,EACf,KAA4B,EAC5B,WAAoB;IAEpB,2GAA2G;IAC3G,IAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC;SAClC,MAAM,CAAC,UAAC,CAAC,IAAK,OAAA,WAAW,CAAC,CAAC,CAAC,KAAK,SAAS,EAA5B,CAA4B,CAAC;SAC3C,IAAI,EAAE,CAAC;4BAIC,GAAG;;QACZ;;;mCAAgG,EAA/F,cAAM,EAAE,aAAK,CAAmF;;IAHnG,iHAAiH;IACjH,2GAA2G;IAC3G,KAAkB,UAAI,EAAJ,aAAI,EAAJ,kBAAI,EAAJ,IAAI;QAAjB,IAAM,GAAG,aAAA;gBAAH,GAAG;KAEb;IAED,uEAAuE;IACvE,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AACzB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"patchTokens.test.d.ts","sourceRoot":"","sources":["../src/patchTokens.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { patchTokens } from './patchTokens';
|
|
2
|
+
import { getMemoCache } from '@fluentui-react-native/memo-cache';
|
|
3
|
+
var themeTokens = {
|
|
4
|
+
uno: 'uno',
|
|
5
|
+
dos: 'dos',
|
|
6
|
+
tres: 3,
|
|
7
|
+
quatro: 4,
|
|
8
|
+
cinco: true,
|
|
9
|
+
};
|
|
10
|
+
describe('patchTokens tests', function () {
|
|
11
|
+
test('props get copied', function () {
|
|
12
|
+
var cache = getMemoCache();
|
|
13
|
+
var patchValues = { uno: 'one', quatro: 'quatro' };
|
|
14
|
+
var tokens = patchTokens(themeTokens, cache, patchValues)[0];
|
|
15
|
+
expect(tokens).not.toBe(themeTokens);
|
|
16
|
+
for (var key in patchValues) {
|
|
17
|
+
expect(tokens[key]).toEqual(patchValues[key]);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
test('no copied props does not change tokens', function () {
|
|
21
|
+
var cache = getMemoCache();
|
|
22
|
+
var patchValues1 = {};
|
|
23
|
+
var tokens = patchTokens(themeTokens, cache, patchValues1)[0];
|
|
24
|
+
expect(tokens).toBe(themeTokens);
|
|
25
|
+
var patchValues2 = { tres: undefined };
|
|
26
|
+
var tokens2 = patchTokens(themeTokens, cache, patchValues2)[0];
|
|
27
|
+
expect(tokens2).toBe(themeTokens);
|
|
28
|
+
});
|
|
29
|
+
test('patching tokens cache independent of order', function () {
|
|
30
|
+
var cache = getMemoCache();
|
|
31
|
+
var patch1 = { uno: 'one', cinco: false };
|
|
32
|
+
var patch2 = { cinco: false, uno: 'one' };
|
|
33
|
+
var tokens1 = patchTokens(themeTokens, cache, patch1)[0];
|
|
34
|
+
var tokens2 = patchTokens(themeTokens, cache, patch2)[0];
|
|
35
|
+
expect(tokens1).toBe(tokens2);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
//# sourceMappingURL=patchTokens.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"patchTokens.test.js","sourceRoot":"","sources":["../src/patchTokens.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAUjE,IAAM,WAAW,GAAW;IAC1B,GAAG,EAAE,KAAK;IACV,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,CAAC;IACP,MAAM,EAAE,CAAC;IACT,KAAK,EAAE,IAAI;CACZ,CAAC;AAEF,QAAQ,CAAC,mBAAmB,EAAE;IAC5B,IAAI,CAAC,kBAAkB,EAAE;QACvB,IAAM,KAAK,GAAG,YAAY,EAAE,CAAC;QAC7B,IAAM,WAAW,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;QAC9C,IAAA,wDAAM,CAAiD;QAC9D,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QACrC,KAAK,IAAM,GAAG,IAAI,WAAW,EAAE;YAC7B,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC;SAC/C;IACH,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,wCAAwC,EAAE;QAC7C,IAAM,KAAK,GAAG,YAAY,EAAE,CAAC;QAC7B,IAAM,YAAY,GAAG,EAAE,CAAC;QACjB,IAAA,yDAAM,CAAkD;QAC/D,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAEjC,IAAM,YAAY,GAAG,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;QAClC,IAAA,0DAAO,CAAkD;QAChE,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,4CAA4C,EAAE;QACjD,IAAM,KAAK,GAAG,YAAY,EAAE,CAAC;QAC7B,IAAM,MAAM,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;QAC5C,IAAM,MAAM,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;QACrC,IAAA,oDAAO,CAA4C;QACnD,IAAA,oDAAO,CAA4C;QAC1D,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTokens.samples.test.d.ts","sourceRoot":"","sources":["../src/useTokens.samples.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
13
|
+
var t = {};
|
|
14
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
15
|
+
t[p] = s[p];
|
|
16
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
17
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
18
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
19
|
+
t[p[i]] = s[p[i]];
|
|
20
|
+
}
|
|
21
|
+
return t;
|
|
22
|
+
};
|
|
23
|
+
import { Text, View } from 'react-native';
|
|
24
|
+
import toJson from 'enzyme-to-json';
|
|
25
|
+
import * as React from 'react';
|
|
26
|
+
import { mount } from 'enzyme';
|
|
27
|
+
import { buildUseTokens } from './buildUseTokens';
|
|
28
|
+
import { mergeStyles } from '@fluentui-react-native/merge-props';
|
|
29
|
+
import { immutableMerge } from '@fluentui-react-native/immutable-merge';
|
|
30
|
+
/**
|
|
31
|
+
* The default/base theme just contains base values
|
|
32
|
+
*/
|
|
33
|
+
var baseTheme = {
|
|
34
|
+
globals: {
|
|
35
|
+
backgroundColor: 'white',
|
|
36
|
+
color: 'black',
|
|
37
|
+
borderColor: 'blue',
|
|
38
|
+
fontFamily: 'Arial',
|
|
39
|
+
fontSize: 12,
|
|
40
|
+
},
|
|
41
|
+
components: {},
|
|
42
|
+
};
|
|
43
|
+
var current = { theme: baseTheme };
|
|
44
|
+
var useTheme = function () { return current.theme; };
|
|
45
|
+
var setActiveTheme = function (theme) {
|
|
46
|
+
current.theme = (theme && immutableMerge(baseTheme, theme)) || baseTheme;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* this wrapper solves the (so-far) inexplicable type errors from the matchers in typescript
|
|
50
|
+
*/
|
|
51
|
+
function snapshotTestTree(tree) {
|
|
52
|
+
expect(toJson(tree)).toMatchSnapshot();
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Helper function used to look up a component in the theme. Having this injected allows this module to not be dependent on the shape of
|
|
56
|
+
* the theme used.
|
|
57
|
+
*/
|
|
58
|
+
var getComponentInfo = function (theme, name) { return theme.components[name]; };
|
|
59
|
+
describe('useTokens samples', function () {
|
|
60
|
+
/**
|
|
61
|
+
* Sample #1 - Themeable text element
|
|
62
|
+
*
|
|
63
|
+
* This adds some default opinions for how a text element should be styled but only allows for customization
|
|
64
|
+
* via theming
|
|
65
|
+
*/
|
|
66
|
+
var useTokensSample1 = buildUseTokens(getComponentInfo,
|
|
67
|
+
/** first the default values should come from the global theme section */
|
|
68
|
+
function (t) { return ({
|
|
69
|
+
color: t.globals.color,
|
|
70
|
+
fontFamily: t.globals.fontFamily,
|
|
71
|
+
fontSize: t.globals.fontSize,
|
|
72
|
+
}); },
|
|
73
|
+
/** next we should look for a component reference to overlay */
|
|
74
|
+
'SampleText');
|
|
75
|
+
var SampleText1 = function (props) {
|
|
76
|
+
// standard props splitting
|
|
77
|
+
var style = props.style, children = props.children, rest = __rest(props, ["style", "children"]);
|
|
78
|
+
// typically this would start with a call to retrieve the theme from the context via whatever method is appropriate
|
|
79
|
+
var theme = useTheme();
|
|
80
|
+
// next the tokens are resolved from the theme, a cache specific to this theme is returned as well to allow for
|
|
81
|
+
// style objects to not be rebuilt unnecessarily
|
|
82
|
+
var _a = useTokensSample1(theme), tokens = _a[0], cache = _a[1];
|
|
83
|
+
// build up the text style, or the full props as appropriate
|
|
84
|
+
var styleFromTokens = cache(
|
|
85
|
+
/**
|
|
86
|
+
* first build the style object
|
|
87
|
+
* - this executes once for every unique set of keys.
|
|
88
|
+
* - The cache is already unique for this theme
|
|
89
|
+
*/
|
|
90
|
+
function () { return (__assign({}, tokens)); },
|
|
91
|
+
/**
|
|
92
|
+
* now specify the keys
|
|
93
|
+
* - because the only changing variable is the theme
|
|
94
|
+
* - ...and all the style properties are either constant or come from the tokens
|
|
95
|
+
* - ...no keys need to be specified
|
|
96
|
+
* - this means that only one style object will be created per component + theme pair
|
|
97
|
+
*/
|
|
98
|
+
[]);
|
|
99
|
+
// merge the props from the tokens with anything passed in via style. This is internally cached via object identity
|
|
100
|
+
// so the merged style object won't change identity unless one of the two inputs changes identity.
|
|
101
|
+
var mergedStyle = mergeStyles(styleFromTokens, style);
|
|
102
|
+
// now just render the element, forwarding the props, setting the merged style, then passing the children as appropriate
|
|
103
|
+
return (React.createElement(Text, __assign({}, rest, { style: mergedStyle }), children));
|
|
104
|
+
};
|
|
105
|
+
beforeEach(function () {
|
|
106
|
+
setActiveTheme();
|
|
107
|
+
});
|
|
108
|
+
/** first render the component with no updates */
|
|
109
|
+
it('Sample1Text rendering with no overrides', function () {
|
|
110
|
+
var tree = mount(React.createElement(SampleText1, null, "Sample1a"));
|
|
111
|
+
snapshotTestTree(tree);
|
|
112
|
+
});
|
|
113
|
+
/** now re-theme the component via the components in the theme */
|
|
114
|
+
it('Sample1Text rendering with some custom settings in the theme', function () {
|
|
115
|
+
setActiveTheme({
|
|
116
|
+
components: {
|
|
117
|
+
SampleText: {
|
|
118
|
+
color: 'pink',
|
|
119
|
+
fontSize: 24,
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
var tree = mount(React.createElement(SampleText1, null, "Sample1b"));
|
|
124
|
+
snapshotTestTree(tree);
|
|
125
|
+
});
|
|
126
|
+
// the Sample2Text component is built the same way as sample1, just using the new hook that has been created
|
|
127
|
+
var SampleText2 = function (props) {
|
|
128
|
+
var color = props.color, style = props.style, children = props.children, rest = __rest(props, ["color", "style", "children"]);
|
|
129
|
+
var theme = useTheme();
|
|
130
|
+
// this starts the same as sample1, extract tokens from the theme and get a theme specific cache object
|
|
131
|
+
var _a = useTokensSample1(theme), tokens = _a[0], cache = _a[1];
|
|
132
|
+
// now when building up the style this time, the resulting style object is based upon both the theme and the passed
|
|
133
|
+
// in value of colors. Because the theme is already part of the cache definition, only color needs to be a key
|
|
134
|
+
var styleFromTokens = cache(
|
|
135
|
+
/** build the style, only patch the color if it has a value, otherwise the theme value would get stomped if color was undefined */
|
|
136
|
+
function () { return (__assign(__assign({}, tokens), (color && { color: color }))); },
|
|
137
|
+
/** use color as an additional key for the style */
|
|
138
|
+
[color]);
|
|
139
|
+
// now just render, this time merging styles inline to make it a bit shorter
|
|
140
|
+
return (React.createElement(Text, __assign({}, rest, { style: mergeStyles(styleFromTokens, style) }), children));
|
|
141
|
+
};
|
|
142
|
+
/** rendering the Sample2 component with the base theme */
|
|
143
|
+
it('Sample2Text rendering with defaults and a color override', function () {
|
|
144
|
+
var tree = mount(React.createElement(View, null,
|
|
145
|
+
React.createElement(SampleText2, null, "Sample2 with defaults"),
|
|
146
|
+
React.createElement(SampleText2, { color: "green" }, "Sample2 with color override via prop")));
|
|
147
|
+
snapshotTestTree(tree);
|
|
148
|
+
});
|
|
149
|
+
/** now re-theme the component via the components in the theme */
|
|
150
|
+
it('Sample2Text rendering with some custom settings in the theme', function () {
|
|
151
|
+
setActiveTheme({
|
|
152
|
+
components: {
|
|
153
|
+
SampleText: {
|
|
154
|
+
fontSize: 18,
|
|
155
|
+
fontFamily: 'Helvetica',
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
var tree = mount(React.createElement(View, null,
|
|
160
|
+
React.createElement(SampleText2, null, "Sample2 with theme overrides set"),
|
|
161
|
+
React.createElement(SampleText2, { color: "purple" }, "Sample2 with theme and color prop override")));
|
|
162
|
+
snapshotTestTree(tree);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
//# sourceMappingURL=useTokens.samples.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+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Ac,CAAC;YACb,UAAU,EAAE;gBACV,UAAU,EAAE;oBACV,QAAQ,EAAE,EAAE;oBACZ,UAAU,EAAE,WAAW;iBACxB;aACF;SACF,CAAC,CAAC;QACH,IAAM,IAAI,GAAG,KAAK,CAChB,oBAAC,IAAI;YACH,oBAAC,WAAW,2CAA+C;YAC3D,oBAAC,WAAW,IAAC,KAAK,EAAC,QAAQ,iDAAyD,CAC/E,CACR,CAAC;QACF,gBAAgB,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { GetMemoValue } from '@fluentui-react-native/memo-cache';
|
|
2
|
+
export declare function applyPropsToTokens<TProps, TTokens>(props: TProps, tokens: TTokens, cache: GetMemoValue<TTokens>, keys: (keyof TTokens)[]): [TTokens, GetMemoValue<TTokens>];
|
|
3
|
+
//# sourceMappingURL=applyPropsToTokens.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"applyPropsToTokens.d.ts","sourceRoot":"","sources":["../src/applyPropsToTokens.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAEjE,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,OAAO,EAChD,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,OAAO,EACf,KAAK,EAAE,YAAY,CAAC,OAAO,CAAC,EAC5B,IAAI,EAAE,CAAC,MAAM,OAAO,CAAC,EAAE,GACtB,CAAC,OAAO,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,CAOlC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
function applyPropsToTokens(props, tokens, cache, keys) {
|
|
15
|
+
var _loop_1 = function (key) {
|
|
16
|
+
var _a;
|
|
17
|
+
var sourceValue = props[key];
|
|
18
|
+
var setValue = sourceValue === tokens[key] ? undefined : sourceValue;
|
|
19
|
+
_a = cache(function () {
|
|
20
|
+
var _a;
|
|
21
|
+
return (setValue === undefined ? tokens : __assign(__assign({}, tokens), (_a = {}, _a[key] = setValue, _a)));
|
|
22
|
+
}, [setValue]), tokens = _a[0], cache = _a[1];
|
|
23
|
+
};
|
|
24
|
+
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
25
|
+
var key = keys_1[_i];
|
|
26
|
+
_loop_1(key);
|
|
27
|
+
}
|
|
28
|
+
return [tokens, cache];
|
|
29
|
+
}
|
|
30
|
+
exports.applyPropsToTokens = applyPropsToTokens;
|
|
31
|
+
//# sourceMappingURL=applyPropsToTokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"applyPropsToTokens.js","sourceRoot":"","sources":["../src/applyPropsToTokens.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAEA,SAAgB,kBAAkB,CAChC,KAAa,EACb,MAAe,EACf,KAA4B,EAC5B,IAAuB;4BAEZ,GAAG;;QACZ,IAAM,WAAW,GAAG,KAAK,CAAC,GAAa,CAAC,CAAC;QACzC,IAAM,QAAQ,GAAG,WAAW,KAAK,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC;QACvE;;;sBAA6G,EAA5G,cAAM,EAAE,aAAK,CAAgG;;IAHhH,KAAkB,UAAI,EAAJ,aAAI,EAAJ,kBAAI,EAAJ,IAAI;QAAjB,IAAM,GAAG,aAAA;gBAAH,GAAG;KAIb;IACD,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AACzB,CAAC;AAZD,gDAYC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"applyPropsToTokens.test.d.ts","sourceRoot":"","sources":["../src/applyPropsToTokens.test.ts"],"names":[],"mappings":""}
|