@dxos/ui-theme 0.8.4-main.c85a9c8dae → 0.8.4-main.d05539e30a
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/LICENSE +102 -5
- package/README.md +1 -1
- package/dist/lib/browser/index.mjs +423 -499
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +423 -499
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/plugin/node-cjs/{theme.css → main.css} +204 -125
- package/dist/plugin/node-cjs/main.css.map +7 -0
- package/dist/plugin/node-cjs/meta.json +1 -1
- package/dist/plugin/node-cjs/plugins/ThemePlugin.cjs +8 -15
- package/dist/plugin/node-cjs/plugins/ThemePlugin.cjs.map +3 -3
- package/dist/plugin/node-esm/{theme.css → main.css} +204 -125
- package/dist/plugin/node-esm/main.css.map +7 -0
- package/dist/plugin/node-esm/meta.json +1 -1
- package/dist/plugin/node-esm/plugins/ThemePlugin.mjs +8 -15
- package/dist/plugin/node-esm/plugins/ThemePlugin.mjs.map +3 -3
- package/dist/types/src/Theme.stories.d.ts.map +1 -1
- package/dist/types/src/defs.d.ts +2 -2
- package/dist/types/src/defs.d.ts.map +1 -1
- package/dist/types/src/fragments/density.d.ts +1 -4
- package/dist/types/src/fragments/density.d.ts.map +1 -1
- package/dist/types/src/fragments/hover.d.ts +0 -1
- package/dist/types/src/fragments/hover.d.ts.map +1 -1
- package/dist/types/src/fragments/index.d.ts +0 -5
- package/dist/types/src/fragments/index.d.ts.map +1 -1
- package/dist/types/src/fragments/text.d.ts +0 -5
- package/dist/types/src/fragments/text.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/plugins/ThemePlugin.d.ts +1 -1
- package/dist/types/src/plugins/ThemePlugin.d.ts.map +1 -1
- package/dist/types/src/theme/components/avatar.d.ts.map +1 -1
- package/dist/types/src/theme/components/button.d.ts +2 -2
- package/dist/types/src/theme/components/button.d.ts.map +1 -1
- package/dist/types/src/theme/components/card.d.ts +3 -2
- package/dist/types/src/theme/components/card.d.ts.map +1 -1
- package/dist/types/src/theme/components/dialog.d.ts.map +1 -1
- package/dist/types/src/theme/components/focus.d.ts +6 -0
- package/dist/types/src/theme/components/focus.d.ts.map +1 -0
- package/dist/types/src/theme/components/icon-button.d.ts +1 -0
- package/dist/types/src/theme/components/icon-button.d.ts.map +1 -1
- package/dist/types/src/theme/components/icon.d.ts +3 -0
- package/dist/types/src/theme/components/icon.d.ts.map +1 -1
- package/dist/types/src/theme/components/index.d.ts +1 -0
- package/dist/types/src/theme/components/index.d.ts.map +1 -1
- package/dist/types/src/theme/components/input.d.ts +8 -8
- package/dist/types/src/theme/components/input.d.ts.map +1 -1
- package/dist/types/src/theme/components/link.d.ts.map +1 -1
- package/dist/types/src/theme/components/list.d.ts.map +1 -1
- package/dist/types/src/theme/components/main.d.ts.map +1 -1
- package/dist/types/src/theme/components/message.d.ts.map +1 -1
- package/dist/types/src/theme/components/popover.d.ts.map +1 -1
- package/dist/types/src/theme/components/scroll-area.d.ts +12 -2
- package/dist/types/src/theme/components/scroll-area.d.ts.map +1 -1
- package/dist/types/src/theme/components/select.d.ts.map +1 -1
- package/dist/types/src/theme/components/status.d.ts +1 -1
- package/dist/types/src/theme/components/status.d.ts.map +1 -1
- package/dist/types/src/theme/components/toast.d.ts.map +1 -1
- package/dist/types/src/theme/components/toolbar.d.ts +0 -1
- package/dist/types/src/theme/components/toolbar.d.ts.map +1 -1
- package/dist/types/src/theme/components/tooltip.d.ts.map +1 -1
- package/dist/types/src/theme/components/treegrid.d.ts.map +1 -1
- package/dist/types/src/theme/index.d.ts +1 -0
- package/dist/types/src/theme/index.d.ts.map +1 -1
- package/dist/types/src/theme/primitives/column.d.ts +25 -3
- package/dist/types/src/theme/primitives/column.d.ts.map +1 -1
- package/dist/types/src/theme/primitives/panel.d.ts +9 -5
- package/dist/types/src/theme/primitives/panel.d.ts.map +1 -1
- package/dist/types/src/theme/theme.d.ts.map +1 -1
- package/dist/types/src/util/elevation.d.ts.map +1 -0
- package/dist/types/src/util/hash-styles.d.ts +1 -1
- package/dist/types/src/util/hash-styles.d.ts.map +1 -1
- package/dist/types/src/util/index.d.ts +3 -0
- package/dist/types/src/util/index.d.ts.map +1 -1
- package/dist/types/src/util/mx.d.ts +52 -4
- package/dist/types/src/util/mx.d.ts.map +1 -1
- package/dist/types/src/util/size.d.ts +27 -0
- package/dist/types/src/util/size.d.ts.map +1 -0
- package/dist/types/src/util/valence.d.ts +4 -0
- package/dist/types/src/util/valence.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -18
- package/src/Theme.stories.tsx +5 -5
- package/src/css/DESIGN_SYSTEM.md +159 -0
- package/src/css/base/base.css +2 -2
- package/src/css/components/button.css +4 -3
- package/src/css/components/checkbox.css +1 -1
- package/src/css/components/{focus-ring.css → focus.css} +27 -13
- package/src/css/components/icon.css +9 -0
- package/src/css/components/link.css +3 -0
- package/src/css/components/panel.css +45 -45
- package/src/css/components/selected.css +76 -0
- package/src/css/components/selected.md +101 -0
- package/src/css/components/surface.css +4 -4
- package/src/css/components/tag.css +25 -23
- package/src/css/integrations/codemirror.css +5 -3
- package/src/css/integrations/tldraw.css +1 -0
- package/src/css/layout/main.css +0 -7
- package/src/css/layout/size.css +19 -27
- package/src/css/theme/animation.css +31 -0
- package/src/css/theme/palette.css +8 -0
- package/src/css/theme/semantic.css +48 -36
- package/src/css/theme/spacing.css +37 -20
- package/src/css/theme/styles.css +22 -22
- package/src/css/utilities.css +114 -3
- package/src/defs.ts +2 -2
- package/src/fragments/AUDIT.md +55 -0
- package/src/fragments/density.ts +8 -5
- package/src/fragments/hover.ts +0 -2
- package/src/fragments/index.ts +1 -6
- package/src/fragments/text.ts +1 -6
- package/src/index.ts +1 -1
- package/src/{theme.css → main.css} +48 -10
- package/src/plugins/ThemePlugin.ts +12 -24
- package/src/plugins/main.css +45 -0
- package/src/theme/components/avatar.ts +3 -4
- package/src/theme/components/button.ts +3 -3
- package/src/theme/components/card.ts +19 -11
- package/src/theme/components/dialog.ts +4 -3
- package/src/theme/components/focus.ts +33 -0
- package/src/theme/components/icon-button.ts +6 -5
- package/src/theme/components/icon.ts +13 -4
- package/src/theme/components/index.ts +1 -0
- package/src/theme/components/input.ts +18 -33
- package/src/theme/components/link.ts +1 -2
- package/src/theme/components/list.ts +4 -4
- package/src/theme/components/menu.ts +4 -4
- package/src/theme/components/message.ts +6 -7
- package/src/theme/components/popover.ts +4 -5
- package/src/theme/components/scroll-area.ts +58 -46
- package/src/theme/components/select.ts +3 -4
- package/src/theme/components/status.ts +5 -5
- package/src/theme/components/toast.ts +2 -3
- package/src/theme/components/toolbar.ts +1 -7
- package/src/theme/components/tooltip.ts +2 -3
- package/src/theme/components/treegrid.ts +1 -1
- package/src/theme/index.ts +1 -0
- package/src/theme/primitives/column.ts +49 -8
- package/src/theme/primitives/panel.ts +19 -23
- package/src/theme/theme.ts +2 -0
- package/src/typings.d.ts +3 -0
- package/src/util/hash-styles.ts +19 -19
- package/src/util/index.ts +3 -0
- package/src/util/mx.ts +119 -8
- package/src/util/size.ts +103 -0
- package/dist/plugin/node-cjs/theme.css.map +0 -7
- package/dist/plugin/node-esm/theme.css.map +0 -7
- package/dist/types/src/fragments/elevation.d.ts.map +0 -1
- package/dist/types/src/fragments/focus.d.ts +0 -4
- package/dist/types/src/fragments/focus.d.ts.map +0 -1
- package/dist/types/src/fragments/selected.d.ts +0 -4
- package/dist/types/src/fragments/selected.d.ts.map +0 -1
- package/dist/types/src/fragments/size.d.ts +0 -7
- package/dist/types/src/fragments/size.d.ts.map +0 -1
- package/dist/types/src/fragments/valence.d.ts +0 -4
- package/dist/types/src/fragments/valence.d.ts.map +0 -1
- package/src/fragments/focus.ts +0 -11
- package/src/fragments/selected.ts +0 -12
- package/src/fragments/size.ts +0 -117
- /package/dist/types/src/{fragments → util}/elevation.d.ts +0 -0
- /package/src/{fragments → util}/elevation.ts +0 -0
- /package/src/{fragments → util}/valence.ts +0 -0
package/src/util/hash-styles.ts
CHANGED
|
@@ -11,7 +11,7 @@ export type ColorStyles = {
|
|
|
11
11
|
hue: Hue;
|
|
12
12
|
fill: string; // -fill
|
|
13
13
|
surface: string; // -surface
|
|
14
|
-
|
|
14
|
+
foreground: string; // -foreground
|
|
15
15
|
text: string; // -text
|
|
16
16
|
border: string; // -border
|
|
17
17
|
};
|
|
@@ -20,7 +20,7 @@ const neutral: ColorStyles = {
|
|
|
20
20
|
hue: 'neutral',
|
|
21
21
|
fill: 'bg-neutral-fill',
|
|
22
22
|
surface: 'bg-neutral-surface',
|
|
23
|
-
|
|
23
|
+
foreground: 'text-neutral-foreground',
|
|
24
24
|
text: 'text-neutral-text',
|
|
25
25
|
border: 'border-neutral-border',
|
|
26
26
|
};
|
|
@@ -32,7 +32,7 @@ const styles: ColorStyles[] = [
|
|
|
32
32
|
hue: 'red',
|
|
33
33
|
fill: 'bg-red-fill',
|
|
34
34
|
surface: 'bg-red-surface',
|
|
35
|
-
|
|
35
|
+
foreground: 'text-red-foreground',
|
|
36
36
|
text: 'text-red-text',
|
|
37
37
|
border: 'border-red-border',
|
|
38
38
|
},
|
|
@@ -40,7 +40,7 @@ const styles: ColorStyles[] = [
|
|
|
40
40
|
hue: 'orange',
|
|
41
41
|
fill: 'bg-orange-fill',
|
|
42
42
|
surface: 'bg-orange-surface',
|
|
43
|
-
|
|
43
|
+
foreground: 'text-orange-foreground',
|
|
44
44
|
text: 'text-orange-text',
|
|
45
45
|
border: 'border-orange-border',
|
|
46
46
|
},
|
|
@@ -48,7 +48,7 @@ const styles: ColorStyles[] = [
|
|
|
48
48
|
hue: 'amber',
|
|
49
49
|
fill: 'bg-amber-fill',
|
|
50
50
|
surface: 'bg-amber-surface',
|
|
51
|
-
|
|
51
|
+
foreground: 'text-amber-foreground',
|
|
52
52
|
text: 'text-amber-text',
|
|
53
53
|
border: 'border-amber-border',
|
|
54
54
|
},
|
|
@@ -56,7 +56,7 @@ const styles: ColorStyles[] = [
|
|
|
56
56
|
hue: 'yellow',
|
|
57
57
|
fill: 'bg-yellow-fill',
|
|
58
58
|
surface: 'bg-yellow-surface',
|
|
59
|
-
|
|
59
|
+
foreground: 'text-yellow-foreground',
|
|
60
60
|
text: 'text-yellow-text',
|
|
61
61
|
border: 'border-yellow-border',
|
|
62
62
|
},
|
|
@@ -64,7 +64,7 @@ const styles: ColorStyles[] = [
|
|
|
64
64
|
hue: 'lime',
|
|
65
65
|
fill: 'bg-lime-fill',
|
|
66
66
|
surface: 'bg-lime-surface',
|
|
67
|
-
|
|
67
|
+
foreground: 'text-lime-foreground',
|
|
68
68
|
text: 'text-lime-text',
|
|
69
69
|
border: 'border-lime-border',
|
|
70
70
|
},
|
|
@@ -72,7 +72,7 @@ const styles: ColorStyles[] = [
|
|
|
72
72
|
hue: 'green',
|
|
73
73
|
fill: 'bg-green-fill',
|
|
74
74
|
surface: 'bg-green-surface',
|
|
75
|
-
|
|
75
|
+
foreground: 'text-green-foreground',
|
|
76
76
|
text: 'text-green-text',
|
|
77
77
|
border: 'border-green-border',
|
|
78
78
|
},
|
|
@@ -80,7 +80,7 @@ const styles: ColorStyles[] = [
|
|
|
80
80
|
hue: 'emerald',
|
|
81
81
|
fill: 'bg-emerald-fill',
|
|
82
82
|
surface: 'bg-emerald-surface',
|
|
83
|
-
|
|
83
|
+
foreground: 'text-emerald-foreground',
|
|
84
84
|
text: 'text-emerald-text',
|
|
85
85
|
border: 'border-emerald-border',
|
|
86
86
|
},
|
|
@@ -88,7 +88,7 @@ const styles: ColorStyles[] = [
|
|
|
88
88
|
hue: 'teal',
|
|
89
89
|
fill: 'bg-teal-fill',
|
|
90
90
|
surface: 'bg-teal-surface',
|
|
91
|
-
|
|
91
|
+
foreground: 'text-teal-foreground',
|
|
92
92
|
text: 'text-teal-text',
|
|
93
93
|
border: 'border-teal-border',
|
|
94
94
|
},
|
|
@@ -96,7 +96,7 @@ const styles: ColorStyles[] = [
|
|
|
96
96
|
hue: 'cyan',
|
|
97
97
|
fill: 'bg-cyan-fill',
|
|
98
98
|
surface: 'bg-cyan-surface',
|
|
99
|
-
|
|
99
|
+
foreground: 'text-cyan-foreground',
|
|
100
100
|
text: 'text-cyan-text',
|
|
101
101
|
border: 'border-cyan-border',
|
|
102
102
|
},
|
|
@@ -104,7 +104,7 @@ const styles: ColorStyles[] = [
|
|
|
104
104
|
hue: 'sky',
|
|
105
105
|
fill: 'bg-sky-fill',
|
|
106
106
|
surface: 'bg-sky-surface',
|
|
107
|
-
|
|
107
|
+
foreground: 'text-sky-foreground',
|
|
108
108
|
text: 'text-sky-text',
|
|
109
109
|
border: 'border-sky-border',
|
|
110
110
|
},
|
|
@@ -112,7 +112,7 @@ const styles: ColorStyles[] = [
|
|
|
112
112
|
hue: 'blue',
|
|
113
113
|
fill: 'bg-blue-fill',
|
|
114
114
|
surface: 'bg-blue-surface',
|
|
115
|
-
|
|
115
|
+
foreground: 'text-blue-foreground',
|
|
116
116
|
text: 'text-blue-text',
|
|
117
117
|
border: 'border-blue-border',
|
|
118
118
|
},
|
|
@@ -120,7 +120,7 @@ const styles: ColorStyles[] = [
|
|
|
120
120
|
hue: 'indigo',
|
|
121
121
|
fill: 'bg-indigo-fill',
|
|
122
122
|
surface: 'bg-indigo-surface',
|
|
123
|
-
|
|
123
|
+
foreground: 'text-indigo-foreground',
|
|
124
124
|
text: 'text-indigo-text',
|
|
125
125
|
border: 'border-indigo-border',
|
|
126
126
|
},
|
|
@@ -128,7 +128,7 @@ const styles: ColorStyles[] = [
|
|
|
128
128
|
hue: 'violet',
|
|
129
129
|
fill: 'bg-violet-fill',
|
|
130
130
|
surface: 'bg-violet-surface',
|
|
131
|
-
|
|
131
|
+
foreground: 'text-violet-foreground',
|
|
132
132
|
text: 'text-violet-text',
|
|
133
133
|
border: 'border-violet-border',
|
|
134
134
|
},
|
|
@@ -136,7 +136,7 @@ const styles: ColorStyles[] = [
|
|
|
136
136
|
hue: 'purple',
|
|
137
137
|
fill: 'bg-purple-fill',
|
|
138
138
|
surface: 'bg-purple-surface',
|
|
139
|
-
|
|
139
|
+
foreground: 'text-purple-foreground',
|
|
140
140
|
text: 'text-purple-text',
|
|
141
141
|
border: 'border-purple-border',
|
|
142
142
|
},
|
|
@@ -144,7 +144,7 @@ const styles: ColorStyles[] = [
|
|
|
144
144
|
hue: 'fuchsia',
|
|
145
145
|
fill: 'bg-fuchsia-fill',
|
|
146
146
|
surface: 'bg-fuchsia-surface',
|
|
147
|
-
|
|
147
|
+
foreground: 'text-fuchsia-foreground',
|
|
148
148
|
text: 'text-fuchsia-text',
|
|
149
149
|
border: 'border-fuchsia-border',
|
|
150
150
|
},
|
|
@@ -152,7 +152,7 @@ const styles: ColorStyles[] = [
|
|
|
152
152
|
hue: 'pink',
|
|
153
153
|
fill: 'bg-pink-fill',
|
|
154
154
|
surface: 'bg-pink-surface',
|
|
155
|
-
|
|
155
|
+
foreground: 'text-pink-foreground',
|
|
156
156
|
text: 'text-pink-text',
|
|
157
157
|
border: 'border-pink-border',
|
|
158
158
|
},
|
|
@@ -160,7 +160,7 @@ const styles: ColorStyles[] = [
|
|
|
160
160
|
hue: 'rose',
|
|
161
161
|
fill: 'bg-rose-fill',
|
|
162
162
|
surface: 'bg-rose-surface',
|
|
163
|
-
|
|
163
|
+
foreground: 'text-rose-foreground',
|
|
164
164
|
text: 'text-rose-text',
|
|
165
165
|
border: 'border-rose-border',
|
|
166
166
|
},
|
package/src/util/index.ts
CHANGED
package/src/util/mx.ts
CHANGED
|
@@ -2,9 +2,22 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import {
|
|
6
|
+
Children,
|
|
7
|
+
createElement,
|
|
8
|
+
forwardRef,
|
|
9
|
+
type ForwardRefExoticComponent,
|
|
10
|
+
type ForwardedRef,
|
|
11
|
+
type HTMLAttributes,
|
|
12
|
+
isValidElement,
|
|
13
|
+
type ReactNode,
|
|
14
|
+
type RefAttributes,
|
|
15
|
+
CSSProperties,
|
|
16
|
+
} from 'react';
|
|
5
17
|
import { extendTailwindMerge, validators } from 'tailwind-merge';
|
|
6
18
|
|
|
7
|
-
import {
|
|
19
|
+
import { log } from '@dxos/log';
|
|
20
|
+
import { ThemedClassName, type ComposableProps, type SlottableProps } from '@dxos/ui-types';
|
|
8
21
|
|
|
9
22
|
type AdditionalClassGroups = 'density' | 'dx-focus-ring';
|
|
10
23
|
|
|
@@ -23,7 +36,6 @@ export const mx = extendTailwindMerge<AdditionalClassGroups>({
|
|
|
23
36
|
'font-bold',
|
|
24
37
|
'font-extrabold',
|
|
25
38
|
'font-black',
|
|
26
|
-
|
|
27
39
|
// Arbitrary numbers
|
|
28
40
|
validators.isArbitraryNumber,
|
|
29
41
|
],
|
|
@@ -51,12 +63,111 @@ export const mx = extendTailwindMerge<AdditionalClassGroups>({
|
|
|
51
63
|
});
|
|
52
64
|
|
|
53
65
|
/**
|
|
54
|
-
* Reconciles className properties from slot.
|
|
66
|
+
* Reconciles className properties from a parent slot.
|
|
67
|
+
* - `className` is set by the Slot merge mechanism.
|
|
68
|
+
* - `classNames` is the consumer-facing prop for theming overrides.
|
|
69
|
+
* Use `composableProps` to reconcile both into a single `className`.
|
|
55
70
|
*/
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
71
|
+
// TODO(burdon): Move to react-ui.
|
|
72
|
+
export const composableProps = <P extends HTMLElement = HTMLElement>(
|
|
73
|
+
{ className, classNames, role, style, ...props }: ComposableProps,
|
|
74
|
+
{ classNames: defaultClassNames, ...defaults }: ThemedClassName<Partial<HTMLAttributes<P>>> | undefined = {},
|
|
59
75
|
) => ({
|
|
60
|
-
|
|
61
|
-
|
|
76
|
+
// Default props.
|
|
77
|
+
...(defaults as object),
|
|
78
|
+
|
|
79
|
+
// Spread supplied props.
|
|
80
|
+
...props,
|
|
81
|
+
|
|
82
|
+
// Prefer explicit role, then defaults role, then 'none'.
|
|
83
|
+
role: role ?? defaults.role ?? 'none',
|
|
84
|
+
|
|
85
|
+
// Merge styles.
|
|
86
|
+
style: { ...defaults.style, ...style } as CSSProperties,
|
|
87
|
+
|
|
88
|
+
// Compose classnames.
|
|
89
|
+
className: mx(defaultClassNames, className, classNames),
|
|
62
90
|
});
|
|
91
|
+
|
|
92
|
+
/** Symbol used to mark components created by `composable()` or `slottable()`. */
|
|
93
|
+
const COMPOSABLE = Symbol.for('dxos.composable');
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Factory for slottable components.
|
|
97
|
+
* The implementation receives full `HTMLAttributes<E>` so it can destructure `role`, `style`, etc.
|
|
98
|
+
* Consumers see only `SlottableProps<P>` — a narrow type exposing `classNames`, `className`,
|
|
99
|
+
* `children`, `asChild`, and the custom props `P`.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```tsx
|
|
103
|
+
* const MyPanel = slottable<HTMLDivElement, { border?: boolean }>(
|
|
104
|
+
* ({ children, asChild, border, ...props }, forwardedRef) => {
|
|
105
|
+
* const Comp = asChild ? Slot : Primitive.div;
|
|
106
|
+
* return (
|
|
107
|
+
* <Comp {...composableProps(props, { classNames: border && 'border' })} ref={forwardedRef}>
|
|
108
|
+
* {children}
|
|
109
|
+
* </Comp>
|
|
110
|
+
* );
|
|
111
|
+
* },
|
|
112
|
+
* );
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export function slottable<E extends HTMLElement, P extends object = {}>(
|
|
116
|
+
render: (props: SlottableProps<P> & HTMLAttributes<E>, forwardedRef: ForwardedRef<E>) => ReactNode,
|
|
117
|
+
): ForwardRefExoticComponent<SlottableProps<P> & RefAttributes<E>> {
|
|
118
|
+
const wrapped = (props: SlottableProps<P> & HTMLAttributes<E>, forwardedRef: ForwardedRef<E>) => {
|
|
119
|
+
let warn = false;
|
|
120
|
+
if (props.asChild) {
|
|
121
|
+
try {
|
|
122
|
+
const child = Children.only(props.children);
|
|
123
|
+
if (isValidElement(child) && typeof child.type !== 'string' && !(child.type as any)[COMPOSABLE]) {
|
|
124
|
+
warn = true;
|
|
125
|
+
log.warn('slot child is not composable; create it with composable() or slottable()', {
|
|
126
|
+
child: (child.type as any).displayName ?? (child.type as any).name,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
} catch {
|
|
130
|
+
// Children.only throws if not exactly one child — Slot handles this.
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const result = render(props, forwardedRef);
|
|
135
|
+
if (warn) {
|
|
136
|
+
return createElement('div', { role: 'none', className: 'dx-slot-warning' }, result);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return result;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const component = forwardRef(wrapped as any) as any;
|
|
143
|
+
(component as any)[COMPOSABLE] = true;
|
|
144
|
+
return component;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Factory for composable (leaf) components.
|
|
149
|
+
* The implementation receives full `HTMLAttributes<E>` so it can destructure `role`, `style`, etc.
|
|
150
|
+
* Consumers see only `ComposableProps<P>` — a narrow type exposing `classNames`, `className`,
|
|
151
|
+
* `children`, and the custom props `P`.
|
|
152
|
+
*
|
|
153
|
+
* For generic components, use `any` for the type parameter inside `composable` and
|
|
154
|
+
* cast the result to restore the generic signature for consumers.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```tsx
|
|
158
|
+
* const Leaf = composable<HTMLButtonElement>(({ children, ...props }, forwardedRef) => {
|
|
159
|
+
* return (
|
|
160
|
+
* <button {...composableProps(props, { classNames: 'btn' })} ref={forwardedRef}>
|
|
161
|
+
* {children}
|
|
162
|
+
* </button>
|
|
163
|
+
* );
|
|
164
|
+
* });
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export function composable<E extends HTMLElement, P extends object = {}>(
|
|
168
|
+
render: (props: ComposableProps<P> & HTMLAttributes<E>, forwardedRef: ForwardedRef<E>) => ReactNode,
|
|
169
|
+
): ForwardRefExoticComponent<ComposableProps<P> & RefAttributes<E>> {
|
|
170
|
+
const component = forwardRef(render as any) as any;
|
|
171
|
+
(component as any)[COMPOSABLE] = true;
|
|
172
|
+
return component;
|
|
173
|
+
}
|
package/src/util/size.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2022 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type CSSProperties } from 'react';
|
|
6
|
+
|
|
7
|
+
import { type Size } from '@dxos/ui-types';
|
|
8
|
+
|
|
9
|
+
import { mx } from '../util';
|
|
10
|
+
|
|
11
|
+
// NOTE: Class names must be fully-specified string literals so Tailwind's static scanner can detect them.
|
|
12
|
+
const sizeMap: Record<string, { w: string; h: string }> = {
|
|
13
|
+
0: { w: 'w-0', h: 'h-0' },
|
|
14
|
+
px: { w: 'w-px', h: 'h-px' },
|
|
15
|
+
0.5: { w: 'w-0.5', h: 'h-0.5' },
|
|
16
|
+
1: { w: 'w-1', h: 'h-1' },
|
|
17
|
+
1.5: { w: 'w-1.5', h: 'h-1.5' },
|
|
18
|
+
2: { w: 'w-2', h: 'h-2' },
|
|
19
|
+
2.5: { w: 'w-2.5', h: 'h-2.5' },
|
|
20
|
+
3: { w: 'w-3', h: 'h-3' },
|
|
21
|
+
3.5: { w: 'w-3.5', h: 'h-3.5' },
|
|
22
|
+
4: { w: 'w-4', h: 'h-4' },
|
|
23
|
+
5: { w: 'w-5', h: 'h-5' },
|
|
24
|
+
6: { w: 'w-6', h: 'h-6' },
|
|
25
|
+
7: { w: 'w-7', h: 'h-7' },
|
|
26
|
+
8: { w: 'w-8', h: 'h-8' },
|
|
27
|
+
9: { w: 'w-9', h: 'h-9' },
|
|
28
|
+
10: { w: 'w-10', h: 'h-10' },
|
|
29
|
+
11: { w: 'w-11', h: 'h-11' },
|
|
30
|
+
12: { w: 'w-12', h: 'h-12' },
|
|
31
|
+
14: { w: 'w-14', h: 'h-14' },
|
|
32
|
+
16: { w: 'w-16', h: 'h-16' },
|
|
33
|
+
20: { w: 'w-20', h: 'h-20' },
|
|
34
|
+
24: { w: 'w-24', h: 'h-24' },
|
|
35
|
+
28: { w: 'w-28', h: 'h-28' },
|
|
36
|
+
32: { w: 'w-32', h: 'h-32' },
|
|
37
|
+
36: { w: 'w-36', h: 'h-36' },
|
|
38
|
+
40: { w: 'w-40', h: 'h-40' },
|
|
39
|
+
44: { w: 'w-44', h: 'h-44' },
|
|
40
|
+
48: { w: 'w-48', h: 'h-48' },
|
|
41
|
+
52: { w: 'w-52', h: 'h-52' },
|
|
42
|
+
56: { w: 'w-56', h: 'h-56' },
|
|
43
|
+
60: { w: 'w-60', h: 'h-60' },
|
|
44
|
+
64: { w: 'w-64', h: 'h-64' },
|
|
45
|
+
72: { w: 'w-72', h: 'h-72' },
|
|
46
|
+
80: { w: 'w-80', h: 'h-80' },
|
|
47
|
+
96: { w: 'w-96', h: 'h-96' },
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const SIZE_VALUES: Size[] = Object.keys(sizeMap).map((key) => (key === 'px' ? 'px' : (Number(key) as Size)));
|
|
51
|
+
|
|
52
|
+
export const getHeight = (size: Size) => sizeMap[size]?.h;
|
|
53
|
+
export const getWidth = (size: Size) => sizeMap[size]?.w;
|
|
54
|
+
export const getSize = (size: Size) => mx(getHeight(size), getWidth(size));
|
|
55
|
+
|
|
56
|
+
export const sizeValue = (size: Size): number => (size === 'px' ? 1 : size);
|
|
57
|
+
export const sizeToRem = (size: Size): string => (size === 'px' ? '1px' : `${(size as number) * 0.25}rem`);
|
|
58
|
+
|
|
59
|
+
export const iconSize = (size: Size | null): CSSProperties =>
|
|
60
|
+
({ '--icon-size': size ? sizeToRem(size) : 'initial' }) as CSSProperties;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Snaps an arbitrary numeric value to the nearest valid Tailwind size token.
|
|
64
|
+
*
|
|
65
|
+
* The function tries progressively coarser series in order until a match is found:
|
|
66
|
+
* 1. Exact match — returns the value as-is if it is already a valid `Size`.
|
|
67
|
+
* 2. Zero or negative — clamps to `0`.
|
|
68
|
+
* 3. Sub-half values (`< 0.5`) — maps to the `'px'` token (1 px).
|
|
69
|
+
* 4. Half-step series (0.5 increments, e.g. 1.5, 2.5 …).
|
|
70
|
+
* 5. Whole-number series (1, 2, 3 …).
|
|
71
|
+
* 6. Double series (even numbers: 2, 4, 6 …).
|
|
72
|
+
* 7. Quadruple series (multiples of 4: 4, 8, 12 …).
|
|
73
|
+
* 8. Falls back to `defaultSize` if no series matches.
|
|
74
|
+
*
|
|
75
|
+
* @param value - Numeric size to snap (in Tailwind spacing units, where 1 unit = 0.25 rem).
|
|
76
|
+
* @param defaultSize - Fallback `Size` token used when no series produces a valid match.
|
|
77
|
+
* @returns The nearest valid `Size` token.
|
|
78
|
+
*/
|
|
79
|
+
export const snapSize = (value: number, defaultSize: Size): Size => {
|
|
80
|
+
if (SIZE_VALUES.includes(value as Size)) {
|
|
81
|
+
return value as Size;
|
|
82
|
+
} else if (value <= 0) {
|
|
83
|
+
return 0;
|
|
84
|
+
} else if (value < 0.5) {
|
|
85
|
+
return 'px';
|
|
86
|
+
} else {
|
|
87
|
+
const wholeSeries = Math.floor(value);
|
|
88
|
+
const halfSeries = Math.floor(value * 2) / 2;
|
|
89
|
+
const doubleSeries = Math.floor(value / 2) * 2;
|
|
90
|
+
const quadrupleSeries = Math.floor(value / 4) * 4;
|
|
91
|
+
if (SIZE_VALUES.includes(halfSeries as Size)) {
|
|
92
|
+
return halfSeries as Size;
|
|
93
|
+
} else if (SIZE_VALUES.includes(wholeSeries as Size)) {
|
|
94
|
+
return wholeSeries as Size;
|
|
95
|
+
} else if (SIZE_VALUES.includes(doubleSeries as Size)) {
|
|
96
|
+
return doubleSeries as Size;
|
|
97
|
+
} else if (SIZE_VALUES.includes(quadrupleSeries as Size)) {
|
|
98
|
+
return quadrupleSeries as Size;
|
|
99
|
+
} else {
|
|
100
|
+
return defaultSize;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|