@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
@@ -0,0 +1,84 @@
1
+ import { CSSObject } from 'styled-components'
2
+
3
+ export interface Context {
4
+ cancelHalfLeadingPx?: number
5
+ hasVerticalPadding?: boolean
6
+ boxShadowTransition?: boolean
7
+ colorTransition?: boolean
8
+ backgroundColorTransition?: boolean
9
+ }
10
+
11
+ /**
12
+ * 絶対にこれを export してはいけない
13
+ *
14
+ * さもないと `o.bg[internalSym]` みたいな叩き方が可能になってしまう(補完にも意図せず出てしまう)
15
+ */
16
+ const internalSym: unique symbol = Symbol('internal')
17
+
18
+ /**
19
+ * CSSObject に変換可能なオブジェクトを作成する
20
+ *
21
+ * 実際に CSSObject に変換するには外部から `__DO_NOT_USE_GET_INTERNAL__` を使わなければならない
22
+ *
23
+ * これ以降メソッドチェーンが続いてもいいし、続かなくても良い
24
+ */
25
+ export function createInternal({
26
+ toCSS,
27
+ context = {},
28
+ }: {
29
+ toCSS: (context: Context) => CSSObject
30
+ context?: Context
31
+ }): Internal {
32
+ return {
33
+ [internalSym]: {
34
+ toCSS,
35
+ context,
36
+ },
37
+ }
38
+ }
39
+
40
+ function __DO_NOT_USE_ACCESS_PRIVATE_PROPERTY__(internal: Internal) {
41
+ return internal[internalSym]
42
+ }
43
+
44
+ export interface Internal {
45
+ [internalSym]: {
46
+ toCSS: (context: Context) => CSSObject
47
+ context: Context
48
+ }
49
+ }
50
+
51
+ // half-leadingをキャンセルするとき && 垂直方向のpaddingが無い時
52
+ // -> before/afterを入れる
53
+ export const shouldCancelHalfLeading = ({
54
+ cancelHalfLeadingPx,
55
+ hasVerticalPadding = false,
56
+ }: Context) => cancelHalfLeadingPx !== undefined && !hasVerticalPadding
57
+
58
+ /**
59
+ * 個別の Internal( o.〇〇 の返り値 )が提出した context の中身を1つの context にまとめる
60
+ */
61
+ export function getContext(internals: Internal[]) {
62
+ return internals.reduce<Context>(
63
+ (context, internal) => ({
64
+ ...context,
65
+ ...__DO_NOT_USE_ACCESS_PRIVATE_PROPERTY__(internal).context,
66
+ }),
67
+ {}
68
+ )
69
+ }
70
+
71
+ /**
72
+ * 全ユーザー定義からコンテキスト生成し、styled-components 向けに CSSObject を構築
73
+ */
74
+ export function toCSSObjects(internals: Internal[]): CSSObject[] {
75
+ // 1パス目
76
+ // 全ユーザー定義を舐めて相互に影響し合う定義をチェックし、その結果(コンテキスト)を取得
77
+ const context = getContext(internals)
78
+
79
+ // 2パス目
80
+ // コンテキストを見ながら最適化されたCSSを構築
81
+ return internals.map((v) =>
82
+ __DO_NOT_USE_ACCESS_PRIVATE_PROPERTY__(v).toCSS(context)
83
+ )
84
+ }
package/src/util.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  import {
2
2
  applyEffect,
3
3
  customPropertyToken,
4
+ disabledSelector,
4
5
  filterObject,
5
6
  flatMapObject,
7
+ notDisabledSelector,
6
8
  } from '@charcoal-ui/utils'
7
- import { CharcoalAbstractTheme } from '@charcoal-ui/theme'
9
+ import { CharcoalAbstractTheme, EffectType, Key } from '@charcoal-ui/theme'
8
10
  import { CSSObject } from 'styled-components'
9
11
 
10
12
  /**
@@ -76,8 +78,23 @@ export function objectAssign<T extends any[]>(...sources: T) {
76
78
  return Object.assign({}, ...sources) as ObjectAssign<T>
77
79
  }
78
80
 
79
- export function objectKeys<V extends object, K extends keyof V>(obj: V) {
80
- return Object.keys(obj) as K[]
81
+ /**
82
+ * Object.keys の返り値の型を厳しめにしてくれるやつ。
83
+ *
84
+ * ジェネリクスは基本的に明示して使うことを推奨。
85
+ *
86
+ * このライブラリでは Theme オブジェクトのジェネリクスを引き回すケースが多く、
87
+ * ジェネリクスを省略するといつのまにか keys の返り値が `string | number | symbol` になりがちなので
88
+ *
89
+ * @param obj - キーを取りたいオブジェクト。ジェネリクスを省略したとき `never[]` のような使えない型が返って欲しい
90
+ */
91
+ export function keyof<
92
+ // このジェネリクスは必須(書かないと返り値が `never[]` になる )
93
+ T,
94
+ // このジェネリクスは書かなくて良い、obj の内容から推論される( T と矛盾してはいけない )
95
+ _ extends T = T
96
+ >(obj: _) {
97
+ return Object.keys(obj) as unknown as (keyof T & string)[]
81
98
  }
82
99
 
83
100
  export interface ReadonlyArrayConstructor {
@@ -93,6 +110,15 @@ export function extractNonNullKeys<V, K extends keyof V>(obj: {
93
110
  .map(([k]) => k) as { [key in K]: V[key] extends null ? never : key }[K][]
94
111
  }
95
112
 
113
+ /**
114
+ * 配列じゃなかったら配列にする
115
+ */
116
+ export function wrapArray<T>(value: ArrayOrSingle<T>): T[] {
117
+ return Array.isArray(value) ? value : [value]
118
+ }
119
+
120
+ export type ArrayOrSingle<T> = T | T[]
121
+
96
122
  export const noThemeProvider = new Error(
97
123
  '`theme` is invalid. `<ThemeProvider>` is not likely mounted.'
98
124
  )
@@ -143,3 +169,20 @@ export function defineThemeVariables(
143
169
  ])
144
170
  }
145
171
  }
172
+
173
+ export function isSupportedEffect(effect: Key): effect is EffectType {
174
+ return ['hover', 'press', 'disabled'].includes(effect as string)
175
+ }
176
+
177
+ export const variable = (value: string) => `var(${value})`
178
+
179
+ export function onEffectPseudo(effect: EffectType, css: CSSObject) {
180
+ return effect === 'hover'
181
+ ? { '&:hover': { [notDisabledSelector]: css } }
182
+ : effect === 'press'
183
+ ? { '&:active': { [notDisabledSelector]: css } }
184
+ : // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
185
+ effect === 'disabled'
186
+ ? { [disabledSelector]: css }
187
+ : unreachable(effect)
188
+ }
package/dist/lib.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,oBAAoB,CAAA;AAGxC;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,OAAO,0LAa0C,CAAA;AAE9D;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,iBAAiB,yQAwB3B,CAAA;AAEH;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,YAAY;;0DAKP,CAAA;AAElB;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,eAAe,uHAapB,CAAA;AAER,oBAAY,QAAQ,CAAC,OAAO,EAAE,UAAU,SAAS,GAAG,IAAI,OAAO,GAAG;IAChE,QAAQ,EAAE,GAAG,IAAI,UAAU,GAAG,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;CAC1E,CAAA;AAED;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,yBAAyB,6NAuB9B,CAAA;AAER,oBAAY,kBAAkB,CAC5B,OAAO,EACP,UAAU,SAAS,MAAM,EACzB,UAAU,SAAS,OAAO,EAAE,IAC1B,OAAO,GAAG;IACZ,QAAQ,EAAE,GAAG,IAAI,UAAU,GAAG,CAC5B,GAAG,IAAI,EAAE,UAAU,KAChB,kBAAkB,CAAC,OAAO,EAAE,OAAO,CAAC,UAAU,EAAE,GAAG,CAAC,EAAE,UAAU,CAAC;CACvE,CAAA;AAED,eAAO,MAAM,QAAQ,UAAW,MAAM,WAAoB,CAAA"}