@charcoal-ui/styled 2.4.0 → 2.6.0

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 (67) hide show
  1. package/dist/SetThemeScript.d.ts +20 -20
  2. package/dist/TokenInjector.d.ts +12 -12
  3. package/dist/TokenInjector.d.ts.map +1 -1
  4. package/dist/builders/border.d.ts +10 -0
  5. package/dist/builders/border.d.ts.map +1 -0
  6. package/dist/builders/borderRadius.d.ts +7 -0
  7. package/dist/builders/borderRadius.d.ts.map +1 -0
  8. package/dist/builders/colors.d.ts +13 -0
  9. package/dist/builders/colors.d.ts.map +1 -0
  10. package/dist/builders/elementEffect.d.ts +7 -0
  11. package/dist/builders/elementEffect.d.ts.map +1 -0
  12. package/dist/builders/o.d.ts +114 -0
  13. package/dist/builders/o.d.ts.map +1 -0
  14. package/dist/builders/outline.d.ts +10 -0
  15. package/dist/builders/outline.d.ts.map +1 -0
  16. package/dist/builders/size.d.ts +23 -0
  17. package/dist/builders/size.d.ts.map +1 -0
  18. package/dist/builders/spacing.d.ts +15 -0
  19. package/dist/builders/spacing.d.ts.map +1 -0
  20. package/dist/builders/transition.d.ts +7 -0
  21. package/dist/builders/transition.d.ts.map +1 -0
  22. package/dist/builders/typography.d.ts +11 -0
  23. package/dist/builders/typography.d.ts.map +1 -0
  24. package/dist/defineThemeVariables.test.d.ts +1 -1
  25. package/dist/{lib.d.ts → factories/lib.d.ts} +88 -89
  26. package/dist/factories/lib.d.ts.map +1 -0
  27. package/dist/helper.d.ts +38 -38
  28. package/dist/helper.d.ts.map +1 -1
  29. package/dist/index.cjs.js +918 -0
  30. package/dist/index.cjs.js.map +1 -0
  31. package/dist/index.d.ts +128 -67
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.esm.js +886 -0
  34. package/dist/index.esm.js.map +1 -0
  35. package/dist/index.story.d.ts +25 -24
  36. package/dist/index.story.d.ts.map +1 -1
  37. package/dist/index.test.d.ts +2 -0
  38. package/dist/index.test.d.ts.map +1 -0
  39. package/dist/internals/index.d.ts +42 -0
  40. package/dist/internals/index.d.ts.map +1 -0
  41. package/dist/util.d.ts +100 -66
  42. package/dist/util.d.ts.map +1 -1
  43. package/package.json +19 -16
  44. package/src/__snapshots__/index.test.tsx.snap +768 -0
  45. package/src/builders/border.ts +63 -0
  46. package/src/builders/borderRadius.ts +32 -0
  47. package/src/builders/colors.ts +198 -0
  48. package/src/builders/elementEffect.ts +54 -0
  49. package/src/builders/o.ts +36 -0
  50. package/src/builders/outline.ts +79 -0
  51. package/src/builders/size.ts +61 -0
  52. package/src/builders/spacing.ts +113 -0
  53. package/src/builders/transition.ts +32 -0
  54. package/src/builders/typography.ts +97 -0
  55. package/src/{lib.ts → factories/lib.ts} +30 -25
  56. package/src/index.story.tsx +2 -2
  57. package/src/index.test.tsx +24 -0
  58. package/src/index.ts +36 -696
  59. package/src/internals/index.ts +84 -0
  60. package/src/util.ts +46 -3
  61. package/dist/index.cjs +0 -1051
  62. package/dist/index.cjs.map +0 -1
  63. package/dist/index.modern.js +0 -826
  64. package/dist/index.modern.js.map +0 -1
  65. package/dist/index.module.js +0 -1034
  66. package/dist/index.module.js.map +0 -1
  67. package/dist/lib.d.ts.map +0 -1
package/dist/index.cjs DELETED
@@ -1,1051 +0,0 @@
1
- var warning = require('warning');
2
- var utils = require('@charcoal-ui/utils');
3
- var foundation = require('@charcoal-ui/foundation');
4
- var React = require('react');
5
- var styledComponents = require('styled-components');
6
-
7
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
-
9
- var warning__default = /*#__PURE__*/_interopDefaultLegacy(warning);
10
- var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
11
-
12
- function _extends() {
13
- _extends = Object.assign ? Object.assign.bind() : function (target) {
14
- for (var i = 1; i < arguments.length; i++) {
15
- var source = arguments[i];
16
-
17
- for (var key in source) {
18
- if (Object.prototype.hasOwnProperty.call(source, key)) {
19
- target[key] = source[key];
20
- }
21
- }
22
- }
23
-
24
- return target;
25
- };
26
- return _extends.apply(this, arguments);
27
- }
28
-
29
- function _taggedTemplateLiteralLoose(strings, raw) {
30
- if (!raw) {
31
- raw = strings.slice(0);
32
- }
33
-
34
- strings.raw = raw;
35
- return strings;
36
- }
37
-
38
- function unreachable(value) {
39
- throw new Error(arguments.length === 0 ? 'unreachable' : "unreachable (" + JSON.stringify(value) + ")");
40
- }
41
- /**
42
- * Check whether a value is non-null and non-undefined
43
- *
44
- * @param value nullable
45
- */
46
-
47
- var isPresent = function isPresent(value) {
48
- return value != null;
49
- }; // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
-
51
- function objectAssign() {
52
- return Object.assign.apply(Object, [{}].concat([].slice.call(arguments)));
53
- }
54
- function objectKeys(obj) {
55
- return Object.keys(obj);
56
- }
57
- var noThemeProvider = new Error('`theme` is invalid. `<ThemeProvider>` is not likely mounted.');
58
- /**
59
- * 子孫要素で使われるカラーテーマの CSS Variables を上書きする
60
- *
61
- * @params colorParams - 上書きしたい色の定義( `theme.color` の一部だけ書けば良い )
62
- * @params effectParams - effect の定義を上書きしたい場合は渡す(必須ではない)
63
- *
64
- * @example
65
- * ```tsx
66
- * const LocalTheme = styled.div`
67
- * ${defineThemeVariables({ text1: '#ff0000' })}
68
- * // `text1` is now defined as red
69
- * ${theme((o) => [o.font.text1])}
70
- * `
71
- * ```
72
- */
73
-
74
- function defineThemeVariables(colorParams, effectParams) {
75
- return function toCssObject(props) {
76
- if (!isPresent(props.theme)) {
77
- throw noThemeProvider;
78
- }
79
-
80
- var colors = utils.filterObject(colorParams, isPresent); // flatMapObject の中で毎回 Object.entries を呼ぶのは無駄なので外で呼ぶ
81
-
82
- var effects = Object.entries(_extends({}, props.theme.effect, effectParams));
83
- return utils.flatMapObject(colors, function (colorKey, color) {
84
- return [[utils.customPropertyToken(colorKey), color]].concat(effects.map(function (_ref3) {
85
- var effectKey = _ref3[0],
86
- effect = _ref3[1];
87
- return [utils.customPropertyToken(colorKey, [effectKey]), utils.applyEffect(color, [effect])];
88
- }));
89
- });
90
- };
91
- }
92
-
93
- /**
94
- * 配列で指定したプロパティを動的に生やす
95
- *
96
- * @param source 拡張するオブジェクト
97
- * @param member オブジェクトに生やすプロパティ一覧
98
- * @param chain プロパティに格納される値を生成する関数
99
- *
100
- * @example
101
- *
102
- * const o = factory({}, ['red', 'blue'],
103
- * color => hex(color)
104
- * )
105
- *
106
- * console.log(o.red) //=> #ff0000
107
- */
108
-
109
- var factory = function factory(source, member, chain) {
110
- return Object.defineProperties(source, Object.fromEntries(member.map(function (key) {
111
- return [key, {
112
- get: function get() {
113
- return chain(key);
114
- },
115
- enumerable: true,
116
- configurable: true
117
- }];
118
- })));
119
- };
120
- /**
121
- * 配列で指定した名前のメソッドを動的に生やす
122
- *
123
- * @param source 拡張するオブジェクト
124
- * @param member オブジェクトに生やすメソッド名一覧
125
- * @param chain メソッドの戻り値になる値を生成する関数
126
- *
127
- * @example
128
- *
129
- * const o = argumentedFactory({}, ['red', 'blue'],
130
- * (color, alpha: number) => hex(color, alpha)
131
- * )
132
- *
133
- * console.log(o.red(0.5)) //=> #ff000077
134
- */
135
-
136
- var argumentedFactory = function argumentedFactory(source, member, chain) {
137
- return Object.defineProperties(source, Object.fromEntries(member.map(function (key) {
138
- return [key, {
139
- value: function value() {
140
- return chain.apply(void 0, [key].concat([].slice.call(arguments)));
141
- },
142
- enumerable: true,
143
- configurable: true
144
- }];
145
- })));
146
- };
147
- /**
148
- * オブジェクトで指定したプロパティ名と値を動的に生やす
149
- *
150
- * @param source 拡張するオブジェクト
151
- * @param def オブジェクトに生やす定義(プロパティ名と値)
152
- *
153
- * @example
154
- *
155
- * const o = constFactory({}, {
156
- * red: '#f00',
157
- * blue: '#00f',
158
- * })
159
- *
160
- * console.log(o.red) //=> #f00
161
- */
162
-
163
- var constFactory = function constFactory(source, def) {
164
- return factory(source, Object.keys(def), function (key) {
165
- return def[key];
166
- });
167
- };
168
- /**
169
- * 配列で指定したモディファイア(プロパティ)をチェーン可能な再帰オブジェクトを動的に生やす
170
- *
171
- * @param modifiers オブジェクトに生やすモディファイヤ一覧
172
- * @param source 指定されたモディファイヤの一覧から値を生成する関数
173
- *
174
- * @example
175
- *
176
- * const o = modifiedArgumentedFactory(['red', 'blue'],
177
- * modifiers => modifiers.map(color => hex(color)).join(',')
178
- * )
179
- *
180
- * console.log(o.red.blue) => #f00,#00f
181
- */
182
-
183
- var modifiedFactory = function modifiedFactory(modifiers, source) {
184
- return function recursiveModifiedFactory(applied) {
185
- var notApplied = modifiers.filter(function (v) {
186
- return !applied.includes(v);
187
- });
188
- return factory(source(applied), notApplied, function (modifier) {
189
- return notApplied.length === 0 ? unreachable() : recursiveModifiedFactory([].concat(applied, [modifier]));
190
- });
191
- }([]);
192
- };
193
- /**
194
- * 配列で指定したモディファイア(メソッド)をチェーン可能な再帰オブジェクトを動的に生やす
195
- *
196
- * @param modifiers オブジェクトに生やすモディファイヤ一覧
197
- * @param source 指定されたモディファイヤの一覧から値を生成する関数
198
- * @param _inferPhantom 関数形式のモディファイヤの引数型を推論するためのメタタイプ(引数の個数に合わせてタプルで指定する)
199
- *
200
- * @example
201
- *
202
- * const o = modifiedArgumentedFactory(['red', 'blue'],
203
- * modifiers => modifiers.map(([color, alpha]) => hex(color, alpha)).join(',')
204
- * , {} as [number])
205
- *
206
- * console.log(o.red(0.5).blue(1)) => #ff000077,#0000ffff
207
- */
208
-
209
- var modifiedArgumentedFactory = function modifiedArgumentedFactory(modifiers, source) {
210
- return function recursiveModifiedFactory(applied) {
211
- var notApplied = modifiers.filter(function (v) {
212
- return !applied.map(function (_ref) {
213
- var w = _ref[0];
214
- return w;
215
- }).includes(v);
216
- });
217
- return argumentedFactory(source(applied), notApplied, function (modifier) {
218
- return notApplied.length === 0 ? unreachable() : recursiveModifiedFactory([].concat(applied, [[modifier].concat([].slice.call(arguments, 1))]));
219
- });
220
- }([]);
221
- };
222
- var variable = function variable(value) {
223
- return "var(" + value + ")";
224
- };
225
-
226
- var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5;
227
- var GlobalStyle = styledComponents.createGlobalStyle(_templateObject || (_templateObject = _taggedTemplateLiteralLoose(["\n ", "\n"])), function (_ref) {
228
- var themeMap = _ref.themeMap,
229
- background = _ref.background;
230
- return Object.entries(themeMap).map(function (_ref2) {
231
- var key = _ref2[0],
232
- theme = _ref2[1];
233
- return key.startsWith('@media') ? styledComponents.css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteralLoose(["\n ", " {\n :root {\n ", "\n ", "\n }\n }\n "])), key, background !== undefined && styledComponents.css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteralLoose(["\n background-color: ", ";\n "])), theme.color[background]), defineColorVariableCSS(theme)) : styledComponents.css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteralLoose(["\n /* stylelint-disable-next-line no-duplicate-selectors */\n ", " {\n ", "\n ", "\n }\n "])), key, background !== undefined && styledComponents.css(_templateObject5 || (_templateObject5 = _taggedTemplateLiteralLoose(["\n background-color: ", ";\n "])), theme.color[background]), defineColorVariableCSS(theme));
234
- });
235
- });
236
- function TokenInjector(_ref3) {
237
- var themeMap = _ref3.theme,
238
- background = _ref3.background;
239
- return /*#__PURE__*/React__default["default"].createElement(GlobalStyle, {
240
- themeMap: themeMap,
241
- background: background
242
- });
243
- }
244
-
245
- var defineColorVariableCSS = function defineColorVariableCSS(theme) {
246
- return Object.entries(defineThemeVariables(theme.color)({
247
- theme: theme
248
- })).map(function (_ref4) {
249
- var varName = _ref4[0],
250
- value = _ref4[1];
251
- return variableDefinition(varName, value.toString());
252
- }).join(';');
253
- };
254
-
255
- var variableDefinition = function variableDefinition(prop, value) {
256
- return prop + ": " + value;
257
- };
258
-
259
- var LOCAL_STORAGE_KEY = 'charcoal-theme';
260
- var DEFAULT_ROOT_ATTRIBUTE = 'theme';
261
- var keyStringRegExp = new RegExp(/^(\w|-)+$/);
262
- /**
263
- * 文字列が英数字_-のみで構成されているか検証する。不正な文字列ならエラーを投げる
264
- * @param key 検証するキー
265
- */
266
-
267
- function assertKeyString(key) {
268
- if (!keyStringRegExp.test(key)) {
269
- throw new Error("Unexpected key :" + key + ", expect: /^(\\w|-)+$/");
270
- }
271
- }
272
- /**
273
- * `<html data-theme="dark">` のような設定を行うデフォルトのセッター
274
- */
275
-
276
- var themeSetter = function themeSetter(attr) {
277
- if (attr === void 0) {
278
- attr = DEFAULT_ROOT_ATTRIBUTE;
279
- }
280
-
281
- return function (theme) {
282
- assertKeyString(attr);
283
-
284
- if (theme !== undefined) {
285
- document.documentElement.dataset[attr] = theme;
286
- } else {
287
- delete document.documentElement.dataset[attr];
288
- }
289
- };
290
- };
291
- /**
292
- * `<html data-theme="dark">` にマッチするセレクタを生成する
293
- */
294
-
295
- function themeSelector(theme, attr) {
296
- return ":root[data-" + (attr != null ? attr : DEFAULT_ROOT_ATTRIBUTE) + "='" + theme + "']";
297
- }
298
- /**
299
- * prefers-color-scheme を利用する media クエリを生成する
300
- */
301
-
302
- function prefersColorScheme(theme) {
303
- return "@media (prefers-color-scheme: " + theme + ")";
304
- }
305
- /**
306
- * LocalStorageからテーマの情報を取得して、変化時にテーマをセットするhooks
307
- */
308
-
309
- function useThemeSetter(_temp) {
310
- var _ref = _temp === void 0 ? {} : _temp,
311
- _ref$key = _ref.key,
312
- key = _ref$key === void 0 ? LOCAL_STORAGE_KEY : _ref$key,
313
- _ref$setter = _ref.setter,
314
- setter = _ref$setter === void 0 ? themeSetter() : _ref$setter;
315
-
316
- var _useTheme = useTheme(key),
317
- theme = _useTheme[0],
318
- system = _useTheme[2];
319
-
320
- React.useEffect(function () {
321
- if (theme === undefined) {
322
- return;
323
- } // prefers-color-scheme から値を取っている場合にはcssのみで処理したいのでアンセットする
324
-
325
-
326
- setter(system ? undefined : theme);
327
- }, [setter, system, theme]);
328
- }
329
- /**
330
- * 同期的にLocalStorageからテーマを取得するヘルパ
331
- */
332
-
333
- function getThemeSync(key) {
334
- if (key === void 0) {
335
- key = LOCAL_STORAGE_KEY;
336
- }
337
-
338
- var theme = localStorage.getItem(key);
339
- return theme;
340
- }
341
- /**
342
- * LocalStorage, prefers-color-scheme からテーマの情報を取得して、現在のテーマを返すhooks
343
- *
344
- * `dark` `light` という名前だけは特別扱いされていて、prefers-color-schemeにマッチした場合に返ります
345
- */
346
-
347
- var useTheme = function useTheme(key) {
348
- if (key === void 0) {
349
- key = LOCAL_STORAGE_KEY;
350
- }
351
-
352
- assertKeyString(key);
353
- var isDark = useMedia('(prefers-color-scheme: dark)');
354
- var media = isDark !== undefined ? isDark ? 'dark' : 'light' : undefined;
355
-
356
- var _useLocalStorage = useLocalStorage(key),
357
- local = _useLocalStorage[0],
358
- setTheme = _useLocalStorage[1],
359
- ready = _useLocalStorage[2];
360
-
361
- var theme = !ready || media === undefined ? undefined : local != null ? local : media;
362
- var system = local === undefined;
363
- return [theme, setTheme, system];
364
- };
365
- function useLocalStorage(key, defaultValue) {
366
- var _useState = React.useState(false),
367
- ready = _useState[0],
368
- setReady = _useState[1];
369
-
370
- var _useState2 = React.useState(),
371
- state = _useState2[0],
372
- setState = _useState2[1];
373
-
374
- var defaultValueMemo = React.useMemo(function () {
375
- return defaultValue == null ? void 0 : defaultValue();
376
- }, [defaultValue]);
377
- React.useEffect(function () {
378
- fetch();
379
- window.addEventListener('storage', handleStorage);
380
- return function () {
381
- window.removeEventListener('storage', handleStorage);
382
- };
383
- });
384
-
385
- var handleStorage = function handleStorage(e) {
386
- if (e.storageArea !== localStorage) {
387
- return;
388
- }
389
-
390
- if (e.key !== key) {
391
- return;
392
- }
393
-
394
- fetch();
395
- };
396
-
397
- var fetch = function fetch() {
398
- var _ref2;
399
-
400
- var raw = localStorage.getItem(key);
401
- setState((_ref2 = raw !== null ? deserialize(raw) : null) != null ? _ref2 : defaultValueMemo);
402
- setReady(true);
403
- };
404
-
405
- var set = function set(value) {
406
- if (value === undefined) {
407
- // undefinedがセットされる場合にはkeyごと削除
408
- localStorage.removeItem(key);
409
- } else {
410
- var raw = serialize(value);
411
- localStorage.setItem(key, raw);
412
- } // 同一ウィンドウではstorageイベントが発火しないので、手動で発火させる
413
-
414
-
415
- var event = new StorageEvent('storage', {
416
- bubbles: true,
417
- cancelable: false,
418
- key: key,
419
- url: location.href,
420
- storageArea: localStorage
421
- });
422
- dispatchEvent(event);
423
- };
424
-
425
- return [state != null ? state : defaultValueMemo, set, ready];
426
- }
427
-
428
- function deserialize(raw) {
429
- try {
430
- return JSON.parse(raw);
431
- } catch (_unused) {
432
- // syntax error はすべて文字列として扱う
433
- return raw;
434
- }
435
- }
436
-
437
- function serialize(value) {
438
- if (typeof value === 'string') {
439
- return value;
440
- } else {
441
- return JSON.stringify(value);
442
- }
443
- }
444
-
445
- function useMedia(query) {
446
- var _useState3 = React.useState(),
447
- match = _useState3[0],
448
- setState = _useState3[1];
449
-
450
- React.useEffect(function () {
451
- var matcher = window.matchMedia(query);
452
-
453
- var onChange = function onChange() {
454
- setState(matcher.matches);
455
- };
456
-
457
- matcher.addEventListener('change', onChange);
458
- setState(matcher.matches);
459
- return function () {
460
- matcher.removeEventListener('change', onChange);
461
- };
462
- }, [query]);
463
- return match;
464
- }
465
-
466
- /**
467
- * 同期的にテーマをローカルストレージから取得してhtmlの属性に設定するコードを取得する
468
- * @param props localStorageのキー、htmlのdataになる属性のキーを含むオブジェクト
469
- * @returns ソースコードの文字列
470
- */
471
-
472
- function makeSetThemeScriptCode(_temp) {
473
- var _ref = _temp === void 0 ? defaultProps : _temp,
474
- _ref$localStorageKey = _ref.localStorageKey,
475
- localStorageKey = _ref$localStorageKey === void 0 ? defaultProps.localStorageKey : _ref$localStorageKey,
476
- _ref$rootAttribute = _ref.rootAttribute,
477
- rootAttribute = _ref$rootAttribute === void 0 ? defaultProps.rootAttribute : _ref$rootAttribute;
478
-
479
- assertKeyString(localStorageKey);
480
- assertKeyString(rootAttribute);
481
- return "'use strict';\n(function () {\n var localStorageKey = '" + localStorageKey + "'\n var rootAttribute = '" + rootAttribute + "'\n var currentTheme = localStorage.getItem(localStorageKey);\n if (currentTheme) {\n document.documentElement.dataset[rootAttribute] = currentTheme;\n }\n})();\n";
482
- }
483
- /**
484
- * 同期的にテーマをローカルストレージから取得してhtmlの属性に設定するスクリプトタグ
485
- * @param props localStorageのキー、htmlのdataになる属性のキーを含むオブジェクト
486
- * @returns
487
- */
488
-
489
- function SetThemeScript(props) {
490
- var src = makeSetThemeScriptCode(props);
491
- return /*#__PURE__*/React__default["default"].createElement("script", {
492
- dangerouslySetInnerHTML: {
493
- __html: src
494
- }
495
- });
496
- }
497
- var defaultProps = {
498
- localStorageKey: LOCAL_STORAGE_KEY,
499
- rootAttribute: DEFAULT_ROOT_ATTRIBUTE
500
- };
501
- SetThemeScript.defaultProps = defaultProps;
502
-
503
- var spacingProperties = ['margin', 'padding'];
504
- var spacingDirections = ['top', 'right', 'bottom', 'left', 'vertical', 'horizontal', 'all'];
505
- var fixedProperties = ['width', 'height'];
506
- var borderDirections = ['top', 'right', 'bottom', 'left'];
507
- var outlineType = ['focus'];
508
- /**
509
- * `theme(o => [...])` の `o` の部分を構築する
510
- *
511
- * @param theme テーマオブジェクト
512
- * @param isPhantom 型推論のためだけに使う場合にランタイムコストをゼロにするフラグ
513
- */
514
-
515
- function builder(theme, isPhantom) {
516
- if (isPhantom === void 0) {
517
- isPhantom = false;
518
- }
519
-
520
- if (isPhantom) {
521
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
522
- return {};
523
- }
524
-
525
- var colors = objectKeys(theme.color);
526
- var effects = objectKeys(theme.effect); // 色
527
-
528
- var gradientColors = objectKeys(theme.gradientColor);
529
- var colorCss = createColorCss();
530
- var gradientColorCss = createGradientColorCss(theme);
531
- var colorObject = constFactory({}, {
532
- bg: objectAssign(factory({}, colors, function (color) {
533
- return modifiedFactory(effects, function (modifiers) {
534
- return colorCss('bg', color, modifiers);
535
- });
536
- }), factory({}, gradientColors, function (color) {
537
- return function (direction) {
538
- return modifiedFactory(effects, function (modifiers) {
539
- return gradientColorCss(color, modifiers, direction);
540
- });
541
- };
542
- })),
543
- font: factory({}, colors, function (color) {
544
- return modifiedFactory(effects, function (modifiers) {
545
- return colorCss('font', color, modifiers);
546
- });
547
- })
548
- }); // タイポグラフィ
549
-
550
- var typographyModifiers = [// TODO
551
- 'monospace', 'bold', 'preserveHalfLeading'];
552
- var typographyCss = createTypographyCss(theme);
553
- var typographyObject = factory({}, ['typography'], function (_) {
554
- return function (size) {
555
- return modifiedFactory(typographyModifiers, function (modifiers) {
556
- return typographyCss(size, {
557
- preserveHalfLeading: modifiers.includes('preserveHalfLeading'),
558
- monospace: modifiers.includes('monospace'),
559
- bold: modifiers.includes('bold')
560
- });
561
- });
562
- };
563
- }); // スペーシング
564
-
565
- var spacingCss = createSpacingCss(theme);
566
- var spacingObject = factory({}, spacingProperties, function (spacingProperty) {
567
- return modifiedArgumentedFactory(spacingDirections, function (modifiers) {
568
- return spacingCss(spacingProperty, modifiers);
569
- });
570
- }); // 大きさ
571
-
572
- var fixedPxCss = createFixedPxCss(theme);
573
- var fixedColumnCss = createFixedColumnCss(theme);
574
- var fixedRelativeCss = createFixedRelativeCss();
575
- var fixedObject = factory({}, fixedProperties, function (property) {
576
- return constFactory({}, {
577
- px: function px(size) {
578
- return fixedPxCss(property, size);
579
- },
580
- column: function column(span) {
581
- return fixedColumnCss(property, span);
582
- },
583
- auto: fixedRelativeCss(property, 'auto'),
584
- full: fixedRelativeCss(property, '100%')
585
- });
586
- }); // 要素へのエフェクト (etc: 透過)
587
-
588
- var elementEffectCss = createElementEffectCss(theme);
589
- var elementEffectObject = modifiedFactory(objectKeys(theme.elementEffect), function (modifiers) {
590
- return elementEffectCss(modifiers);
591
- }); // ボーダー
592
-
593
- var borderCss = createBorderCss(theme);
594
- var borderObject = constFactory({}, {
595
- border: factory({}, objectKeys(theme.border), function (variant) {
596
- return modifiedFactory(borderDirections, function (modifiers) {
597
- return borderCss(variant, modifiers);
598
- });
599
- })
600
- }); // 角丸
601
-
602
- var borderRadiusCss = createBorderRadiusCss(theme);
603
- var borderRadiusObject = constFactory({}, {
604
- borderRadius: function borderRadius(radius) {
605
- return borderRadiusCss(radius);
606
- }
607
- }); // アウトライン
608
-
609
- var outlineCss = createOutlineColorCss(theme);
610
- var outlineObject = constFactory({}, {
611
- outline: factory({}, objectKeys(theme.outline), function (variant) {
612
- return modifiedFactory(outlineType, function (modifiers) {
613
- return outlineCss(variant, modifiers);
614
- });
615
- })
616
- });
617
- return objectAssign(colorObject, typographyObject, spacingObject, fixedObject, elementEffectObject, borderObject, borderRadiusObject, outlineObject);
618
- }
619
-
620
- function targetProperty(target) {
621
- return target === 'bg' ? 'background-color' : 'color';
622
- }
623
-
624
- function isSupportedEffect(effect) {
625
- return ['hover', 'press', 'disabled'].includes(effect);
626
- }
627
-
628
- function onEffectPseudo(effect, css) {
629
- var _hover, _active, _ref;
630
-
631
- return effect === 'hover' ? {
632
- '&:hover': (_hover = {}, _hover[utils.notDisabledSelector] = css, _hover)
633
- } : effect === 'press' ? {
634
- '&:active': (_active = {}, _active[utils.notDisabledSelector] = css, _active)
635
- } : // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
636
- effect === 'disabled' ? (_ref = {}, _ref[utils.disabledSelector] = css, _ref) : unreachable(effect);
637
- }
638
-
639
- var createColorCss = function createColorCss(_theme) {
640
- return function (target, color, effects) {
641
- if (effects === void 0) {
642
- effects = [];
643
- }
644
-
645
- return internal(function () {
646
- var _extends2;
647
-
648
- return _extends((_extends2 = {}, _extends2[targetProperty(target)] = variable(utils.customPropertyToken(color.toString())), _extends2), effects.filter(isSupportedEffect).reduce(function (acc, effect) {
649
- var _onEffectPseudo;
650
-
651
- return _extends({}, acc, onEffectPseudo(effect, (_onEffectPseudo = {}, _onEffectPseudo[targetProperty(target)] = variable(utils.customPropertyToken(color.toString(), [effect])), _onEffectPseudo)));
652
- }, {}));
653
- }, effects.length > 0 ? target === 'font' ? {
654
- colorTransition: true
655
- } : {
656
- backgroundColorTransition: true
657
- } : {});
658
- };
659
- }; // TODO: deprecate
660
-
661
-
662
- var TRANSITION_DURATION = 0.2;
663
-
664
- var createGradientColorCss = function createGradientColorCss(theme) {
665
- return function (color, effects, direction) {
666
- if (effects === void 0) {
667
- effects = [];
668
- }
669
-
670
- var toLinearGradient = utils.gradient(direction);
671
- return internal(function (context) {
672
- var optimized = !useHalfLeadingCanceller(context);
673
- var duration = utils.dur(TRANSITION_DURATION);
674
-
675
- if (optimized && effects.length > 0) {
676
- return _extends({
677
- position: 'relative',
678
- zIndex: 0,
679
- overflow: 'hidden'
680
- }, effects.filter(isSupportedEffect).reduce(function (acc, effect) {
681
- var _theme$effect$effect;
682
-
683
- return _extends({}, acc, {
684
- '&::before': _extends({
685
- zIndex: -1
686
- }, overlayElement, {
687
- transition: duration + " background-color"
688
- }),
689
- '&::after': _extends({
690
- zIndex: -2
691
- }, overlayElement, toLinearGradient(theme.gradientColor[color]))
692
- }, onEffectPseudo(effect, {
693
- '&::before': {
694
- backgroundColor: utils.applyEffect(null, (_theme$effect$effect = theme.effect[effect]) != null ? _theme$effect$effect : [])
695
- }
696
- }));
697
- }, {}));
698
- } else {
699
- warning__default["default"](effects.length === 0, // eslint-disable-next-line max-len
700
- "'Transition' will not be applied. You can get around this by specifying 'preserveHalfLeading' or both 'padding' and 'typograpy'.");
701
- return _extends({}, toLinearGradient(theme.gradientColor[color]), effects.filter(isSupportedEffect).reduce(function (acc, effect) {
702
- var _theme$effect$effect2;
703
-
704
- return _extends({}, acc, onEffectPseudo(effect, _extends({}, toLinearGradient(utils.applyEffectToGradient((_theme$effect$effect2 = theme.effect[effect]) != null ? _theme$effect$effect2 : [])(theme.gradientColor[color])))));
705
- }, {}));
706
- }
707
- });
708
- };
709
- };
710
- /**
711
- * @see https://developer.mozilla.org/ja/docs/Web/CSS/:focus-visible#selectively_showing_the_focus_indicator
712
- */
713
-
714
-
715
- var onFocus = function onFocus(css) {
716
- var _ref2;
717
-
718
- return _ref2 = {}, _ref2[utils.notDisabledSelector] = {
719
- '&:focus, &:active': _extends({
720
- outline: 'none'
721
- }, css),
722
- '&:focus:not(:focus-visible), &:active:not(:focus-visible)': {
723
- outline: 'none'
724
- },
725
- '&:focus-visible': _extends({
726
- outline: 'none'
727
- }, css)
728
- }, _ref2;
729
- };
730
-
731
- var outlineCss = function outlineCss(weight, color) {
732
- return {
733
- boxShadow: "0 0 0 " + utils.px(weight) + " " + color
734
- };
735
- };
736
-
737
- var createOutlineColorCss = function createOutlineColorCss(theme) {
738
- return function (variant, modifiers) {
739
- var weight = theme.outline[variant].weight;
740
- var color = theme.outline[variant].color;
741
- return internal(function () {
742
- var _2;
743
-
744
- return modifiers.includes('focus') ? onFocus(outlineCss(weight, color)) : {
745
- '&&': (_2 = {}, _2[utils.notDisabledSelector] = outlineCss(weight, color), _2)
746
- };
747
- }, {
748
- boxShadowTransition: true
749
- });
750
- };
751
- };
752
-
753
- var overlayElement = {
754
- content: "''",
755
- display: 'block',
756
- position: 'absolute',
757
- width: '100%',
758
- height: '100%',
759
- top: 0,
760
- left: 0
761
- }; // half-leadingをキャンセルするとき && 垂直方向のpaddingが無い時
762
- // -> before/afterを入れる
763
-
764
- var useHalfLeadingCanceller = function useHalfLeadingCanceller(_ref3) {
765
- var cancelHalfLeadingPx = _ref3.cancelHalfLeadingPx,
766
- _ref3$hasVerticalPadd = _ref3.hasVerticalPadding,
767
- hasVerticalPadding = _ref3$hasVerticalPadd === void 0 ? false : _ref3$hasVerticalPadd;
768
- return cancelHalfLeadingPx !== undefined && !hasVerticalPadding;
769
- };
770
-
771
- var createTypographyCss = function createTypographyCss(theme) {
772
- return function (size, options) {
773
- if (options === void 0) {
774
- options = {};
775
- }
776
-
777
- var _options = options,
778
- _options$preserveHalf = _options.preserveHalfLeading,
779
- preserveHalfLeading = _options$preserveHalf === void 0 ? false : _options$preserveHalf,
780
- _options$monospace = _options.monospace,
781
- monospace = _options$monospace === void 0 ? false : _options$monospace,
782
- _options$bold = _options.bold,
783
- bold = _options$bold === void 0 ? false : _options$bold;
784
- var descriptor = theme.typography.size[size];
785
- var margin = -utils.halfLeading(descriptor);
786
- return internal(function (context) {
787
- return _extends({
788
- fontSize: utils.px(descriptor.fontSize),
789
- lineHeight: utils.px(descriptor.lineHeight)
790
- }, monospace && {
791
- fontFamily: 'monospace'
792
- }, bold && {
793
- fontWeight: 'bold'
794
- }, useHalfLeadingCanceller(context) && {
795
- // prevent margin collapsing
796
- display: 'flow-root',
797
- // cancel half-leading with negative margin
798
- '&::before': _extends({}, leadingCancel, {
799
- marginTop: utils.px(margin)
800
- }),
801
- '&::after': _extends({}, leadingCancel, {
802
- marginBottom: utils.px(margin)
803
- })
804
- });
805
- }, !preserveHalfLeading ? {
806
- cancelHalfLeadingPx: margin
807
- } : {});
808
- };
809
- };
810
-
811
- var leadingCancel = {
812
- display: 'block',
813
- width: 0,
814
- height: 0,
815
- content: "''"
816
- };
817
-
818
- function spacingProperty(property, direction) {
819
- return property + "-" + direction;
820
- }
821
-
822
- var createSpacingCss = function createSpacingCss(theme) {
823
- return function (property, modifiers) {
824
- var _modifiers$reduce = modifiers.reduce(function (acc, _ref4) {
825
- var direction = _ref4[0],
826
- size = _ref4[1];
827
-
828
- if (direction === 'all') {
829
- acc.top = size;
830
- acc.right = size;
831
- acc.bottom = size;
832
- acc.left = size;
833
- } else if (direction === 'vertical') {
834
- acc.top = size;
835
- acc.bottom = size;
836
- } else if (direction === 'horizontal') {
837
- acc.right = size;
838
- acc.left = size;
839
- } else {
840
- acc[direction] = size;
841
- }
842
-
843
- return acc;
844
- }, {}),
845
- top = _modifiers$reduce.top,
846
- right = _modifiers$reduce.right,
847
- bottom = _modifiers$reduce.bottom,
848
- left = _modifiers$reduce.left;
849
-
850
- var hasVerticalPadding = property === 'padding' && top !== undefined && bottom !== undefined && top !== 'auto' && bottom !== 'auto';
851
- return internal(function (_ref5) {
852
- var _ref6, _ref7, _ref8, _ref9;
853
-
854
- var _ref5$cancelHalfLeadi = _ref5.cancelHalfLeadingPx,
855
- cancelHalfLeadingPx = _ref5$cancelHalfLeadi === void 0 ? 0 : _ref5$cancelHalfLeadi;
856
- return _extends({}, top !== undefined && (_ref6 = {}, _ref6[spacingProperty(property, 'top')] = top === 'auto' ? 'auto' : utils.px(theme.spacing[top] + (hasVerticalPadding ? cancelHalfLeadingPx : 0)), _ref6), bottom !== undefined && (_ref7 = {}, _ref7[spacingProperty(property, 'bottom')] = bottom === 'auto' ? 'auto' : utils.px(theme.spacing[bottom] + (hasVerticalPadding ? cancelHalfLeadingPx : 0)), _ref7), right !== undefined && (_ref8 = {}, _ref8[spacingProperty(property, 'right')] = right === 'auto' ? 'auto' : utils.px(theme.spacing[right]), _ref8), left !== undefined && (_ref9 = {}, _ref9[spacingProperty(property, 'left')] = left === 'auto' ? 'auto' : utils.px(theme.spacing[left]), _ref9));
857
- }, hasVerticalPadding ? {
858
- hasVerticalPadding: true
859
- } : {});
860
- };
861
- };
862
-
863
- var createFixedPxCss = function createFixedPxCss(theme) {
864
- return function (property, size) {
865
- return internal(function () {
866
- var _ref10;
867
-
868
- return _ref10 = {}, _ref10[property] = size === 'auto' ? 'auto' : utils.px(theme.spacing[size]), _ref10;
869
- });
870
- };
871
- };
872
-
873
- var createFixedRelativeCss = function createFixedRelativeCss(_theme) {
874
- return function (property, amount) {
875
- return internal(function () {
876
- var _ref11;
877
-
878
- return _ref11 = {}, _ref11[property] = amount, _ref11;
879
- });
880
- };
881
- };
882
-
883
- var createFixedColumnCss = function createFixedColumnCss(theme) {
884
- return function (property, span) {
885
- return internal(function () {
886
- var _ref12;
887
-
888
- return _ref12 = {}, _ref12[property] = utils.px(foundation.columnSystem(span, theme.grid.unit.column, theme.grid.unit.gutter)), _ref12;
889
- });
890
- };
891
- };
892
-
893
- var createElementEffectCss = function createElementEffectCss(theme) {
894
- return function (effects) {
895
- if (effects === void 0) {
896
- effects = [];
897
- }
898
-
899
- return internal(function () {
900
- return effects.filter(isSupportedEffect).reduce(function (acc, effect) {
901
- var _theme$elementEffect$, _theme$elementEffect$2;
902
-
903
- return _extends({}, acc, onEffectPseudo(effect, {
904
- opacity: !Array.isArray(theme.elementEffect[effect]) && ((_theme$elementEffect$ = theme.elementEffect[effect]) == null ? void 0 : _theme$elementEffect$.type) === 'opacity' ? (_theme$elementEffect$2 = theme.elementEffect[effect]) == null ? void 0 : _theme$elementEffect$2.opacity : unreachable()
905
- }));
906
- }, {});
907
- });
908
- };
909
- };
910
-
911
- function borderProperty(direction) {
912
- return "border-" + direction;
913
- }
914
-
915
- function borderShorthand(color) {
916
- return "solid 1px " + color;
917
- }
918
-
919
- var createBorderCss = function createBorderCss(theme) {
920
- return function (variant, directions) {
921
- var all = directions.length === 0;
922
- var value = borderShorthand(theme.border[variant].color);
923
- return internal(function () {
924
- return _extends({}, all ? {
925
- border: value
926
- } : directions.reduce(function (acc, direction) {
927
- var _extends3;
928
-
929
- return _extends({}, acc, (_extends3 = {}, _extends3[borderProperty(direction)] = value, _extends3));
930
- }, {}));
931
- });
932
- };
933
- };
934
-
935
- var createBorderRadiusCss = function createBorderRadiusCss(theme) {
936
- return function (size) {
937
- return internal(function () {
938
- return {
939
- borderRadius: utils.px(theme.borderRadius[size])
940
- };
941
- });
942
- };
943
- };
944
-
945
- var commonSpec = function commonSpec(_theme) {
946
- var duration = utils.dur(TRANSITION_DURATION);
947
-
948
- var transition = function transition(property) {
949
- return {
950
- transition: property.map(function (v) {
951
- return duration + " " + v;
952
- }).join(', ')
953
- };
954
- };
955
-
956
- return internal(function (_ref13) {
957
- var _ref13$colorTransitio = _ref13.colorTransition,
958
- colorTransition = _ref13$colorTransitio === void 0 ? false : _ref13$colorTransitio,
959
- _ref13$backgroundColo = _ref13.backgroundColorTransition,
960
- backgroundColorTransition = _ref13$backgroundColo === void 0 ? false : _ref13$backgroundColo,
961
- _ref13$boxShadowTrans = _ref13.boxShadowTransition,
962
- boxShadowTransition = _ref13$boxShadowTrans === void 0 ? false : _ref13$boxShadowTrans;
963
- return transition([colorTransition ? 'color' : null, backgroundColorTransition ? 'background-color' : null, boxShadowTransition ? 'box-shadow' : null].filter(isPresent));
964
- });
965
- };
966
-
967
- var internalSym = Symbol('internal');
968
-
969
- function internal(operation, context) {
970
- var _ref14;
971
-
972
- if (context === void 0) {
973
- context = {};
974
- }
975
-
976
- return _ref14 = {}, _ref14[internalSym] = {
977
- operation: operation,
978
- context: context
979
- }, _ref14;
980
- }
981
-
982
- var nonBlank = function nonBlank(value) {
983
- return isPresent(value) && value !== false;
984
- };
985
- /**
986
- * `theme(o => [...])` の `theme` ユーティリティを構築する
987
- *
988
- * @param _styled styled-componnets の `styled` そのもの (型推論のために用いられる)
989
- *
990
- * @example
991
- *
992
- * import styled from 'styled-components'
993
- * const theme = createTheme(styled)
994
- *
995
- * @example
996
- *
997
- * const theme = createTheme<DefaultTheme>()
998
- */
999
-
1000
-
1001
- function createTheme(_styled) {
1002
- // `theme(o => [...])` の `o` の部分の型推論のためだけに使う意味のない変数
1003
- // Tを型変数のまま渡してcreateThemeが呼ばれるまで型の具象化が行われないようにする
1004
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
1005
- builder({}, true); // ランタイムの `theme(o => [...])` のインターフェースを構築する
1006
-
1007
-
1008
- return function ( // ユーザー定義
1009
- spec) {
1010
- return function (_ref15) {
1011
- var theme = _ref15.theme;
1012
-
1013
- if (!isPresent(theme)) {
1014
- // テーマが入っていない場合は復旧不可能なのでエラーにする
1015
- throw noThemeProvider;
1016
- } // styled-componentsのランタイムから受け取ったthemeオブジェクトをbuilderに食わせて`o`をつくる
1017
- // さらに、ユーザー定義にbuilderが構築した`o`を食わせる
1018
- // (`o`を一時変数に入れてしまうと型Tの具象化が行われるので関数合成を優先する)
1019
-
1020
-
1021
- var rawSpecDescriptor = spec(builder(theme)); // ユーザー定義の配列を整形
1022
-
1023
- var specDescriptor = [].concat(Array.isArray(rawSpecDescriptor) ? rawSpecDescriptor : [rawSpecDescriptor], [commonSpec()]).filter(nonBlank); // 1パス目
1024
- // 全ユーザー定義を舐めて相互に影響し合う定義をチェックし、その結果(コンテキスト)を取得
1025
-
1026
- var context = specDescriptor.reduce(function (acc, v) {
1027
- return _extends({}, acc, v[internalSym].context);
1028
- }, {}); // 2パス目
1029
- // コンテキストを見ながら最適化されたCSSを構築
1030
-
1031
- return specDescriptor.map(function (v) {
1032
- return v[internalSym].operation(context);
1033
- });
1034
- };
1035
- };
1036
- }
1037
-
1038
- exports.SetThemeScript = SetThemeScript;
1039
- exports.TokenInjector = TokenInjector;
1040
- exports.createTheme = createTheme;
1041
- exports.defineThemeVariables = defineThemeVariables;
1042
- exports.getThemeSync = getThemeSync;
1043
- exports.makeSetThemeScriptCode = makeSetThemeScriptCode;
1044
- exports.prefersColorScheme = prefersColorScheme;
1045
- exports.themeSelector = themeSelector;
1046
- exports.themeSetter = themeSetter;
1047
- exports.useLocalStorage = useLocalStorage;
1048
- exports.useMedia = useMedia;
1049
- exports.useTheme = useTheme;
1050
- exports.useThemeSetter = useThemeSetter;
1051
- //# sourceMappingURL=index.cjs.map