@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.
- package/dist/builders/border.d.ts +10 -0
- package/dist/builders/border.d.ts.map +1 -0
- package/dist/builders/borderRadius.d.ts +7 -0
- package/dist/builders/borderRadius.d.ts.map +1 -0
- package/dist/builders/colors.d.ts +13 -0
- package/dist/builders/colors.d.ts.map +1 -0
- package/dist/builders/elementEffect.d.ts +7 -0
- package/dist/builders/elementEffect.d.ts.map +1 -0
- package/dist/builders/o.d.ts +115 -0
- package/dist/builders/o.d.ts.map +1 -0
- package/dist/builders/outline.d.ts +10 -0
- package/dist/builders/outline.d.ts.map +1 -0
- package/dist/builders/size.d.ts +23 -0
- package/dist/builders/size.d.ts.map +1 -0
- package/dist/builders/spacing.d.ts +15 -0
- package/dist/builders/spacing.d.ts.map +1 -0
- package/dist/builders/transition.d.ts +7 -0
- package/dist/builders/transition.d.ts.map +1 -0
- package/dist/builders/typography.d.ts +11 -0
- package/dist/builders/typography.d.ts.map +1 -0
- package/dist/{lib.d.ts → factories/lib.d.ts} +13 -14
- package/dist/factories/lib.d.ts.map +1 -0
- package/dist/index.cjs +764 -641
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +91 -30
- package/dist/index.d.ts.map +1 -1
- package/dist/index.modern.js +554 -391
- package/dist/index.modern.js.map +1 -1
- package/dist/index.module.js +764 -641
- package/dist/index.module.js.map +1 -1
- package/dist/index.story.d.ts +1 -0
- package/dist/index.story.d.ts.map +1 -1
- package/dist/index.test.d.ts +2 -0
- package/dist/index.test.d.ts.map +1 -0
- package/dist/internals/index.d.ts +42 -0
- package/dist/internals/index.d.ts.map +1 -0
- package/dist/util.d.ts +36 -2
- package/dist/util.d.ts.map +1 -1
- package/package.json +8 -5
- package/src/__snapshots__/index.test.tsx.snap +768 -0
- package/src/builders/border.ts +63 -0
- package/src/builders/borderRadius.ts +32 -0
- package/src/builders/colors.ts +198 -0
- package/src/builders/elementEffect.ts +54 -0
- package/src/builders/o.ts +43 -0
- package/src/builders/outline.ts +79 -0
- package/src/builders/size.ts +61 -0
- package/src/builders/spacing.ts +113 -0
- package/src/builders/transition.ts +32 -0
- package/src/builders/typography.ts +97 -0
- package/src/{lib.ts → factories/lib.ts} +30 -25
- package/src/index.story.tsx +2 -2
- package/src/index.test.tsx +24 -0
- package/src/index.ts +47 -696
- package/src/internals/index.ts +84 -0
- package/src/util.ts +46 -3
- package/dist/lib.d.ts.map +0 -1
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { CharcoalAbstractTheme } from '@charcoal-ui/theme'
|
|
2
|
+
import { halfLeading, px } from '@charcoal-ui/utils'
|
|
3
|
+
import { CSSObject } from 'styled-components'
|
|
4
|
+
import {
|
|
5
|
+
Internal,
|
|
6
|
+
createInternal,
|
|
7
|
+
shouldCancelHalfLeading,
|
|
8
|
+
Context,
|
|
9
|
+
} from '../internals'
|
|
10
|
+
import { defineProperties, definePropertyChains } from '../factories/lib'
|
|
11
|
+
|
|
12
|
+
export const createTypographyCss =
|
|
13
|
+
<T extends CharcoalAbstractTheme>(theme: T) =>
|
|
14
|
+
(
|
|
15
|
+
size: keyof T['typography']['size'],
|
|
16
|
+
options: {
|
|
17
|
+
preserveHalfLeading?: boolean
|
|
18
|
+
monospace?: boolean
|
|
19
|
+
bold?: boolean
|
|
20
|
+
} = {}
|
|
21
|
+
): Internal => {
|
|
22
|
+
const {
|
|
23
|
+
preserveHalfLeading = false,
|
|
24
|
+
monospace = false,
|
|
25
|
+
bold = false,
|
|
26
|
+
} = options
|
|
27
|
+
const descriptor = theme.typography.size[size]
|
|
28
|
+
const margin = -halfLeading(descriptor)
|
|
29
|
+
|
|
30
|
+
function toCSS(context: Context): CSSObject {
|
|
31
|
+
return {
|
|
32
|
+
fontSize: px(descriptor.fontSize),
|
|
33
|
+
lineHeight: px(descriptor.lineHeight),
|
|
34
|
+
...(monospace && {
|
|
35
|
+
fontFamily: 'monospace',
|
|
36
|
+
}),
|
|
37
|
+
...(bold && {
|
|
38
|
+
fontWeight: 'bold',
|
|
39
|
+
}),
|
|
40
|
+
...(shouldCancelHalfLeading(context) && {
|
|
41
|
+
// prevent margin collapsing
|
|
42
|
+
display: 'flow-root',
|
|
43
|
+
// cancel half-leading with negative margin
|
|
44
|
+
'&::before': {
|
|
45
|
+
...leadingCancel,
|
|
46
|
+
marginTop: px(margin),
|
|
47
|
+
},
|
|
48
|
+
'&::after': {
|
|
49
|
+
...leadingCancel,
|
|
50
|
+
marginBottom: px(margin),
|
|
51
|
+
},
|
|
52
|
+
}),
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return createInternal({
|
|
57
|
+
toCSS,
|
|
58
|
+
context: !preserveHalfLeading
|
|
59
|
+
? {
|
|
60
|
+
cancelHalfLeadingPx: margin,
|
|
61
|
+
}
|
|
62
|
+
: {},
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const leadingCancel: CSSObject = {
|
|
67
|
+
display: 'block',
|
|
68
|
+
width: 0,
|
|
69
|
+
height: 0,
|
|
70
|
+
content: `''`,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// タイポグラフィ
|
|
74
|
+
const typographyModifiers = [
|
|
75
|
+
// TODO
|
|
76
|
+
'monospace',
|
|
77
|
+
'bold',
|
|
78
|
+
'preserveHalfLeading',
|
|
79
|
+
] as const
|
|
80
|
+
|
|
81
|
+
export default function typography<T extends CharcoalAbstractTheme>(theme: T) {
|
|
82
|
+
const typographyCss = createTypographyCss(theme)
|
|
83
|
+
const typographyObject = defineProperties(
|
|
84
|
+
{},
|
|
85
|
+
['typography'] as const,
|
|
86
|
+
(_) => (size: keyof T['typography']['size']) =>
|
|
87
|
+
definePropertyChains(typographyModifiers, (modifiers) =>
|
|
88
|
+
typographyCss(size, {
|
|
89
|
+
preserveHalfLeading: modifiers.includes('preserveHalfLeading'),
|
|
90
|
+
monospace: modifiers.includes('monospace'),
|
|
91
|
+
bold: modifiers.includes('bold'),
|
|
92
|
+
})
|
|
93
|
+
)
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
return typographyObject
|
|
97
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Key } from '@charcoal-ui/theme'
|
|
2
|
-
import { unreachable } from '
|
|
2
|
+
import { unreachable } from '../util'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* 配列で指定したプロパティを動的に生やす
|
|
@@ -16,7 +16,11 @@ import { unreachable } from './util'
|
|
|
16
16
|
*
|
|
17
17
|
* console.log(o.red) //=> #ff0000
|
|
18
18
|
*/
|
|
19
|
-
export const
|
|
19
|
+
export const defineProperties = <
|
|
20
|
+
TSource,
|
|
21
|
+
TMember extends readonly Key[],
|
|
22
|
+
TValue
|
|
23
|
+
>(
|
|
20
24
|
source: TSource,
|
|
21
25
|
member: TMember,
|
|
22
26
|
chain: (key: TMember[number]) => TValue
|
|
@@ -40,13 +44,13 @@ export const factory = <TSource, TMember extends readonly Key[], TValue>(
|
|
|
40
44
|
*
|
|
41
45
|
* @example
|
|
42
46
|
*
|
|
43
|
-
* const o =
|
|
47
|
+
* const o = defineMethods({}, ['red', 'blue'],
|
|
44
48
|
* (color, alpha: number) => hex(color, alpha)
|
|
45
49
|
* )
|
|
46
50
|
*
|
|
47
51
|
* console.log(o.red(0.5)) //=> #ff000077
|
|
48
52
|
*/
|
|
49
|
-
export const
|
|
53
|
+
export const defineMethods = <
|
|
50
54
|
TSource,
|
|
51
55
|
TMember extends readonly string[],
|
|
52
56
|
TValue,
|
|
@@ -80,18 +84,21 @@ export const argumentedFactory = <
|
|
|
80
84
|
*
|
|
81
85
|
* @example
|
|
82
86
|
*
|
|
83
|
-
* const o =
|
|
87
|
+
* const o = defineConstantProperties({}, {
|
|
84
88
|
* red: '#f00',
|
|
85
89
|
* blue: '#00f',
|
|
86
90
|
* })
|
|
87
91
|
*
|
|
88
92
|
* console.log(o.red) //=> #f00
|
|
89
93
|
*/
|
|
90
|
-
export const
|
|
94
|
+
export const defineConstantProperties = <
|
|
95
|
+
TSource,
|
|
96
|
+
TDef extends { [key: string]: unknown }
|
|
97
|
+
>(
|
|
91
98
|
source: TSource,
|
|
92
99
|
def: TDef
|
|
93
100
|
) =>
|
|
94
|
-
|
|
101
|
+
defineProperties(source, Object.keys(def), (key) => def[key]) as TSource &
|
|
95
102
|
Readonly<TDef>
|
|
96
103
|
|
|
97
104
|
/**
|
|
@@ -102,29 +109,29 @@ export const constFactory = <TSource, TDef extends { [key: string]: unknown }>(
|
|
|
102
109
|
*
|
|
103
110
|
* @example
|
|
104
111
|
*
|
|
105
|
-
* const o =
|
|
112
|
+
* const o = definePropertyChains(['red', 'blue'],
|
|
106
113
|
* modifiers => modifiers.map(color => hex(color)).join(',')
|
|
107
114
|
* )
|
|
108
115
|
*
|
|
109
116
|
* console.log(o.red.blue) => #f00,#00f
|
|
110
117
|
*/
|
|
111
|
-
export const
|
|
118
|
+
export const definePropertyChains = <TSource, T extends Key>(
|
|
112
119
|
modifiers: readonly T[],
|
|
113
120
|
source: (applied: readonly T[]) => TSource
|
|
114
121
|
) =>
|
|
115
|
-
(function
|
|
122
|
+
(function definePropertiesRecursively(
|
|
116
123
|
applied: readonly T[]
|
|
117
|
-
):
|
|
124
|
+
): PropertyChain<TSource, T> {
|
|
118
125
|
const notApplied = modifiers.filter((v) => !applied.includes(v))
|
|
119
|
-
return
|
|
126
|
+
return defineProperties(source(applied), notApplied, (modifier) =>
|
|
120
127
|
notApplied.length === 0
|
|
121
128
|
? unreachable()
|
|
122
|
-
:
|
|
129
|
+
: definePropertiesRecursively([...applied, modifier])
|
|
123
130
|
)
|
|
124
131
|
})([])
|
|
125
132
|
|
|
126
|
-
export type
|
|
127
|
-
readonly [key in TModifiers]:
|
|
133
|
+
export type PropertyChain<TSource, TModifiers extends Key> = TSource & {
|
|
134
|
+
readonly [key in TModifiers]: PropertyChain<TSource, Exclude<TModifiers, key>>
|
|
128
135
|
}
|
|
129
136
|
|
|
130
137
|
/**
|
|
@@ -136,13 +143,13 @@ export type Modified<TSource, TModifiers extends Key> = TSource & {
|
|
|
136
143
|
*
|
|
137
144
|
* @example
|
|
138
145
|
*
|
|
139
|
-
* const o =
|
|
146
|
+
* const o = defineMethodChains(['red', 'blue'],
|
|
140
147
|
* modifiers => modifiers.map(([color, alpha]) => hex(color, alpha)).join(',')
|
|
141
148
|
* , {} as [number])
|
|
142
149
|
*
|
|
143
150
|
* console.log(o.red(0.5).blue(1)) => #ff000077,#0000ffff
|
|
144
151
|
*/
|
|
145
|
-
export const
|
|
152
|
+
export const defineMethodChains = <
|
|
146
153
|
TSource,
|
|
147
154
|
T extends string,
|
|
148
155
|
TArguments extends unknown[]
|
|
@@ -151,30 +158,28 @@ export const modifiedArgumentedFactory = <
|
|
|
151
158
|
source: (applied: readonly [T, ...TArguments][]) => TSource,
|
|
152
159
|
..._inferPhantom: TArguments
|
|
153
160
|
) =>
|
|
154
|
-
(function
|
|
161
|
+
(function defineMethodsRecursively(
|
|
155
162
|
applied: readonly [T, ...TArguments][]
|
|
156
|
-
):
|
|
163
|
+
): MethodChain<TSource, T, TArguments> {
|
|
157
164
|
const notApplied = modifiers.filter(
|
|
158
165
|
(v) => !applied.map(([w]) => w).includes(v)
|
|
159
166
|
)
|
|
160
|
-
return
|
|
167
|
+
return defineMethods(
|
|
161
168
|
source(applied),
|
|
162
169
|
notApplied,
|
|
163
170
|
(modifier, ...args: TArguments) =>
|
|
164
171
|
notApplied.length === 0
|
|
165
172
|
? unreachable()
|
|
166
|
-
:
|
|
173
|
+
: defineMethodsRecursively([...applied, [modifier, ...args]])
|
|
167
174
|
)
|
|
168
175
|
})([])
|
|
169
176
|
|
|
170
|
-
export type
|
|
177
|
+
export type MethodChain<
|
|
171
178
|
TSource,
|
|
172
179
|
TModifiers extends string,
|
|
173
180
|
TArguments extends unknown[]
|
|
174
181
|
> = TSource & {
|
|
175
182
|
readonly [key in TModifiers]: (
|
|
176
183
|
...args: TArguments
|
|
177
|
-
) =>
|
|
184
|
+
) => MethodChain<TSource, Exclude<TModifiers, key>, TArguments>
|
|
178
185
|
}
|
|
179
|
-
|
|
180
|
-
export const variable = (value: string) => `var(${value})`
|
package/src/index.story.tsx
CHANGED
|
@@ -21,7 +21,7 @@ type MyTheme = CharcoalTheme & {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
function myTheme(theme: CharcoalTheme): MyTheme {
|
|
24
|
+
export function myTheme(theme: CharcoalTheme): MyTheme {
|
|
25
25
|
return {
|
|
26
26
|
...theme,
|
|
27
27
|
color: {
|
|
@@ -35,7 +35,7 @@ declare module 'styled-components' {
|
|
|
35
35
|
export interface DefaultTheme extends MyTheme {}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
const theme = createTheme(
|
|
38
|
+
const theme = createTheme<DefaultTheme>()
|
|
39
39
|
|
|
40
40
|
export const Example = () => (
|
|
41
41
|
<ThemeProvider theme={myTheme}>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { light } from '@charcoal-ui/theme'
|
|
2
|
+
import 'jest-styled-components'
|
|
3
|
+
|
|
4
|
+
import React from 'react'
|
|
5
|
+
import renderder from 'react-test-renderer'
|
|
6
|
+
import { ThemeProvider } from 'styled-components'
|
|
7
|
+
import { Example, myTheme, TailwindLike } from './index.story'
|
|
8
|
+
|
|
9
|
+
function render(children: JSX.Element) {
|
|
10
|
+
return renderder
|
|
11
|
+
.create(<ThemeProvider theme={myTheme(light)}>{children}</ThemeProvider>)
|
|
12
|
+
.toJSON()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// TODO: もう少し theme() 関数に対する丁寧なユニットテストが欲しい
|
|
16
|
+
describe('Story', () => {
|
|
17
|
+
test('<Example />', () => {
|
|
18
|
+
expect(render(<Example />)).toMatchSnapshot()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('<TailwindLike />', () => {
|
|
22
|
+
expect(render(<TailwindLike />)).toMatchSnapshot()
|
|
23
|
+
})
|
|
24
|
+
})
|