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