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