@choco-vanille/primitives 0.0.0-stage → 0.1.0-dev.2
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 +21 -0
- package/README.md +482 -2
- package/dist/components/_common/components/slot/Slot.cjs +2 -0
- package/dist/components/_common/components/slot/Slot.cjs.map +1 -0
- package/dist/components/_common/components/slot/Slot.js +10 -0
- package/dist/components/_common/components/slot/Slot.js.map +1 -0
- package/dist/components/_common/components/slot/hooks/use-slot/useSlot.hook.cjs +2 -0
- package/dist/components/_common/components/slot/hooks/use-slot/useSlot.hook.cjs.map +1 -0
- package/dist/components/_common/components/slot/hooks/use-slot/useSlot.hook.js +18 -0
- package/dist/components/_common/components/slot/hooks/use-slot/useSlot.hook.js.map +1 -0
- package/dist/components/_common/components/slot/utils/attachRef.utils.cjs +2 -0
- package/dist/components/_common/components/slot/utils/attachRef.utils.cjs.map +1 -0
- package/dist/components/_common/components/slot/utils/attachRef.utils.js +18 -0
- package/dist/components/_common/components/slot/utils/attachRef.utils.js.map +1 -0
- package/dist/components/_common/components/slot/utils/composeRefs.utils.cjs +2 -0
- package/dist/components/_common/components/slot/utils/composeRefs.utils.cjs.map +1 -0
- package/dist/components/_common/components/slot/utils/composeRefs.utils.js +13 -0
- package/dist/components/_common/components/slot/utils/composeRefs.utils.js.map +1 -0
- package/dist/components/_common/components/slot/utils/getSlotChild.utils.cjs +2 -0
- package/dist/components/_common/components/slot/utils/getSlotChild.utils.cjs.map +1 -0
- package/dist/components/_common/components/slot/utils/getSlotChild.utils.js +9 -0
- package/dist/components/_common/components/slot/utils/getSlotChild.utils.js.map +1 -0
- package/dist/components/_common/components/slot/utils/mergeClassNames.utils.cjs +2 -0
- package/dist/components/_common/components/slot/utils/mergeClassNames.utils.cjs.map +1 -0
- package/dist/components/_common/components/slot/utils/mergeClassNames.utils.js +8 -0
- package/dist/components/_common/components/slot/utils/mergeClassNames.utils.js.map +1 -0
- package/dist/components/_common/components/slot/utils/mergeSlotProps.utils.cjs +2 -0
- package/dist/components/_common/components/slot/utils/mergeSlotProps.utils.cjs.map +1 -0
- package/dist/components/_common/components/slot/utils/mergeSlotProps.utils.js +16 -0
- package/dist/components/_common/components/slot/utils/mergeSlotProps.utils.js.map +1 -0
- package/dist/components/_common/components/slot/utils/mergeStyles.utils.cjs +2 -0
- package/dist/components/_common/components/slot/utils/mergeStyles.utils.cjs.map +1 -0
- package/dist/components/_common/components/slot/utils/mergeStyles.utils.js +14 -0
- package/dist/components/_common/components/slot/utils/mergeStyles.utils.js.map +1 -0
- package/dist/components/_common/styles/style-props/styleProps.css.cjs +2 -0
- package/dist/components/_common/styles/style-props/styleProps.css.cjs.map +1 -0
- package/dist/components/_common/styles/style-props/styleProps.css.js +370 -0
- package/dist/components/_common/styles/style-props/styleProps.css.js.map +1 -0
- package/dist/components/_common/utils/environment/isProcessUnavailableError.utils.cjs +2 -0
- package/dist/components/_common/utils/environment/isProcessUnavailableError.utils.cjs.map +1 -0
- package/dist/components/_common/utils/environment/isProcessUnavailableError.utils.js +5 -0
- package/dist/components/_common/utils/environment/isProcessUnavailableError.utils.js.map +1 -0
- package/dist/components/_common/utils/style-props/createStyleSet.utils.cjs +2 -0
- package/dist/components/_common/utils/style-props/createStyleSet.utils.cjs.map +1 -0
- package/dist/components/_common/utils/style-props/createStyleSet.utils.js +13 -0
- package/dist/components/_common/utils/style-props/createStyleSet.utils.js.map +1 -0
- package/dist/components/_common/utils/style-props/formatStyleValue.utils.cjs +2 -0
- package/dist/components/_common/utils/style-props/formatStyleValue.utils.cjs.map +1 -0
- package/dist/components/_common/utils/style-props/formatStyleValue.utils.js +11 -0
- package/dist/components/_common/utils/style-props/formatStyleValue.utils.js.map +1 -0
- package/dist/components/_common/utils/style-props/getClassName.utils.cjs +2 -0
- package/dist/components/_common/utils/style-props/getClassName.utils.cjs.map +1 -0
- package/dist/components/_common/utils/style-props/getClassName.utils.js +21 -0
- package/dist/components/_common/utils/style-props/getClassName.utils.js.map +1 -0
- package/dist/components/_common/utils/style-props/getElementProps.utils.cjs +2 -0
- package/dist/components/_common/utils/style-props/getElementProps.utils.cjs.map +1 -0
- package/dist/components/_common/utils/style-props/getElementProps.utils.js +39 -0
- package/dist/components/_common/utils/style-props/getElementProps.utils.js.map +1 -0
- package/dist/components/_common/utils/style-props/getTokenClassNames.utils.cjs +2 -0
- package/dist/components/_common/utils/style-props/getTokenClassNames.utils.cjs.map +1 -0
- package/dist/components/_common/utils/style-props/getTokenClassNames.utils.js +21 -0
- package/dist/components/_common/utils/style-props/getTokenClassNames.utils.js.map +1 -0
- package/dist/components/_common/utils/style-props/logUnknownStyleValue.utils.cjs +2 -0
- package/dist/components/_common/utils/style-props/logUnknownStyleValue.utils.cjs.map +1 -0
- package/dist/components/_common/utils/style-props/logUnknownStyleValue.utils.js +9 -0
- package/dist/components/_common/utils/style-props/logUnknownStyleValue.utils.js.map +1 -0
- package/dist/components/_common/utils/style-props/resolveSizeValue.utils.cjs +2 -0
- package/dist/components/_common/utils/style-props/resolveSizeValue.utils.cjs.map +1 -0
- package/dist/components/_common/utils/style-props/resolveSizeValue.utils.js +5 -0
- package/dist/components/_common/utils/style-props/resolveSizeValue.utils.js.map +1 -0
- package/dist/components/accessibility/visually-hidden/VisuallyHidden.cjs +2 -0
- package/dist/components/accessibility/visually-hidden/VisuallyHidden.cjs.map +1 -0
- package/dist/components/accessibility/visually-hidden/VisuallyHidden.css.cjs +2 -0
- package/dist/components/accessibility/visually-hidden/VisuallyHidden.css.cjs.map +1 -0
- package/dist/components/accessibility/visually-hidden/VisuallyHidden.css.js +10 -0
- package/dist/components/accessibility/visually-hidden/VisuallyHidden.css.js.map +1 -0
- package/dist/components/accessibility/visually-hidden/VisuallyHidden.js +11 -0
- package/dist/components/accessibility/visually-hidden/VisuallyHidden.js.map +1 -0
- package/dist/components/accessibility/visually-hidden/utils/getVisuallyHiddenElementProps.utils.cjs +2 -0
- package/dist/components/accessibility/visually-hidden/utils/getVisuallyHiddenElementProps.utils.cjs.map +1 -0
- package/dist/components/accessibility/visually-hidden/utils/getVisuallyHiddenElementProps.utils.js +17 -0
- package/dist/components/accessibility/visually-hidden/utils/getVisuallyHiddenElementProps.utils.js.map +1 -0
- package/dist/components/foundation/icon/Icon.cjs +2 -0
- package/dist/components/foundation/icon/Icon.cjs.map +1 -0
- package/dist/components/foundation/icon/Icon.css.cjs +2 -0
- package/dist/components/foundation/icon/Icon.css.cjs.map +1 -0
- package/dist/components/foundation/icon/Icon.css.js +29 -0
- package/dist/components/foundation/icon/Icon.css.js.map +1 -0
- package/dist/components/foundation/icon/Icon.js +8 -0
- package/dist/components/foundation/icon/Icon.js.map +1 -0
- package/dist/components/foundation/icon/utils/getIconElementProps.utils.cjs +2 -0
- package/dist/components/foundation/icon/utils/getIconElementProps.utils.cjs.map +1 -0
- package/dist/components/foundation/icon/utils/getIconElementProps.utils.js +26 -0
- package/dist/components/foundation/icon/utils/getIconElementProps.utils.js.map +1 -0
- package/dist/components/foundation/typography/Typography.cjs +2 -0
- package/dist/components/foundation/typography/Typography.cjs.map +1 -0
- package/dist/components/foundation/typography/Typography.css.cjs +2 -0
- package/dist/components/foundation/typography/Typography.css.cjs.map +1 -0
- package/dist/components/foundation/typography/Typography.css.js +149 -0
- package/dist/components/foundation/typography/Typography.css.js.map +1 -0
- package/dist/components/foundation/typography/Typography.js +11 -0
- package/dist/components/foundation/typography/Typography.js.map +1 -0
- package/dist/components/foundation/typography/utils/getTypographyElementProps.utils.cjs +2 -0
- package/dist/components/foundation/typography/utils/getTypographyElementProps.utils.cjs.map +1 -0
- package/dist/components/foundation/typography/utils/getTypographyElementProps.utils.js +9 -0
- package/dist/components/foundation/typography/utils/getTypographyElementProps.utils.js.map +1 -0
- package/dist/components/layout/box/Box.cjs +2 -0
- package/dist/components/layout/box/Box.cjs.map +1 -0
- package/dist/components/layout/box/Box.css.cjs +2 -0
- package/dist/components/layout/box/Box.css.cjs.map +1 -0
- package/dist/components/layout/box/Box.css.js +418 -0
- package/dist/components/layout/box/Box.css.js.map +1 -0
- package/dist/components/layout/box/Box.js +11 -0
- package/dist/components/layout/box/Box.js.map +1 -0
- package/dist/components/layout/box/utils/getBoxElementProps.utils.cjs +2 -0
- package/dist/components/layout/box/utils/getBoxElementProps.utils.cjs.map +1 -0
- package/dist/components/layout/box/utils/getBoxElementProps.utils.js +9 -0
- package/dist/components/layout/box/utils/getBoxElementProps.utils.js.map +1 -0
- package/dist/components/layout/flex/Flex.cjs +2 -0
- package/dist/components/layout/flex/Flex.cjs.map +1 -0
- package/dist/components/layout/flex/Flex.js +11 -0
- package/dist/components/layout/flex/Flex.js.map +1 -0
- package/dist/components/layout/flex/constants/index.cjs +2 -0
- package/dist/components/layout/flex/constants/index.cjs.map +1 -0
- package/dist/components/layout/flex/constants/index.js +5 -0
- package/dist/components/layout/flex/constants/index.js.map +1 -0
- package/dist/components/layout/flex/utils/getFlexElementProps.utils.cjs +2 -0
- package/dist/components/layout/flex/utils/getFlexElementProps.utils.cjs.map +1 -0
- package/dist/components/layout/flex/utils/getFlexElementProps.utils.js +9 -0
- package/dist/components/layout/flex/utils/getFlexElementProps.utils.js.map +1 -0
- package/dist/components/layout/grid/Grid.cjs +2 -0
- package/dist/components/layout/grid/Grid.cjs.map +1 -0
- package/dist/components/layout/grid/Grid.js +11 -0
- package/dist/components/layout/grid/Grid.js.map +1 -0
- package/dist/components/layout/grid/constants/index.cjs +2 -0
- package/dist/components/layout/grid/constants/index.cjs.map +1 -0
- package/dist/components/layout/grid/constants/index.js +5 -0
- package/dist/components/layout/grid/constants/index.js.map +1 -0
- package/dist/components/layout/grid/utils/getGridElementProps.utils.cjs +2 -0
- package/dist/components/layout/grid/utils/getGridElementProps.utils.cjs.map +1 -0
- package/dist/components/layout/grid/utils/getGridElementProps.utils.js +9 -0
- package/dist/components/layout/grid/utils/getGridElementProps.utils.js.map +1 -0
- package/dist/components/layout/separator/Separator.cjs +2 -0
- package/dist/components/layout/separator/Separator.cjs.map +1 -0
- package/dist/components/layout/separator/Separator.css.cjs +2 -0
- package/dist/components/layout/separator/Separator.css.cjs.map +1 -0
- package/dist/components/layout/separator/Separator.css.js +32 -0
- package/dist/components/layout/separator/Separator.css.js.map +1 -0
- package/dist/components/layout/separator/Separator.js +10 -0
- package/dist/components/layout/separator/Separator.js.map +1 -0
- package/dist/components/layout/separator/utils/getSeparatorElementProps.utils.cjs +2 -0
- package/dist/components/layout/separator/utils/getSeparatorElementProps.utils.cjs.map +1 -0
- package/dist/components/layout/separator/utils/getSeparatorElementProps.utils.js +22 -0
- package/dist/components/layout/separator/utils/getSeparatorElementProps.utils.js.map +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +1427 -0
- package/dist/index.d.ts +1427 -0
- package/dist/index.js +8 -0
- package/dist/styles.css +2 -0
- package/package.json +89 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Choco Vanille contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,483 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @choco-vanille/primitives
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React component library built with [Vanilla Extract](https://vanilla-extract.style/),
|
|
4
|
+
styled with the design tokens of
|
|
5
|
+
[`@choco-vanille/theme`](https://www.npmjs.com/package/@choco-vanille/theme).
|
|
6
|
+
|
|
7
|
+
It provides `Box`, the layout primitive, its `Flex` and `Grid` variants,
|
|
8
|
+
`Separator`, which draws a line between groups of content, `Typography`, the
|
|
9
|
+
text primitive, `Icon`, which draws SVG icons, and `VisuallyHidden`, which
|
|
10
|
+
hides content from the screen but not from screen readers; the other
|
|
11
|
+
components build on them.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @choco-vanille/primitives @choco-vanille/theme
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Requires React 19. `react`, `react-dom` and `@choco-vanille/theme` are peer
|
|
20
|
+
dependencies: install them in your application, which then provides a single
|
|
21
|
+
copy shared with the library.
|
|
22
|
+
|
|
23
|
+
Components are styled with Vanilla Extract: their styles are compiled at
|
|
24
|
+
build time into static CSS, so your application needs no Vanilla Extract
|
|
25
|
+
setup. The small runtime behind dynamic CSS variables
|
|
26
|
+
(`@vanilla-extract/dynamic`) is a dependency of the package, installed
|
|
27
|
+
automatically with it, like `@iconify/react`, which renders icons offline.
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
Components read their colors, spacing and typography from the theme. Import
|
|
32
|
+
the theme stylesheet and the library stylesheet once, and wrap your app in the
|
|
33
|
+
theme's `ThemeProvider`:
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import '@choco-vanille/theme/styles.css';
|
|
37
|
+
import '@choco-vanille/primitives/styles.css';
|
|
38
|
+
|
|
39
|
+
import { ThemeProvider } from '@choco-vanille/theme';
|
|
40
|
+
|
|
41
|
+
<ThemeProvider>
|
|
42
|
+
<App />
|
|
43
|
+
</ThemeProvider>;
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Then import components from the package entry point, which ships ES modules,
|
|
47
|
+
CommonJS and TypeScript declarations. Themes, flavors and tokens are
|
|
48
|
+
configured through the provider, as described in the theme documentation.
|
|
49
|
+
Components are documented in Storybook.
|
|
50
|
+
|
|
51
|
+
### Stylesheet
|
|
52
|
+
|
|
53
|
+
All the CSS of the library ships as a single file,
|
|
54
|
+
`@choco-vanille/primitives/styles.css`, about 41 kB (6 kB gzipped). It is
|
|
55
|
+
not tree-shakable: importing it once, as shown above, brings the styles of
|
|
56
|
+
every component, whatever your application renders.
|
|
57
|
+
|
|
58
|
+
### Cascade layers
|
|
59
|
+
|
|
60
|
+
Every rule of the library stylesheet sits in a cascade layer, under
|
|
61
|
+
`cv.primitives`: `cv.primitives.base` (default styles, such as the style of a
|
|
62
|
+
text tag), then `cv.primitives.props` (style props) and
|
|
63
|
+
`cv.primitives.composites` (style props setting several properties). A style
|
|
64
|
+
prop therefore always overrides a default style, whatever the order of the
|
|
65
|
+
rules.
|
|
66
|
+
|
|
67
|
+
Unlayered styles win over any layer: your own classes always override the
|
|
68
|
+
library, and so does an unlayered CSS reset. If a reset of yours undoes the
|
|
69
|
+
library styles, put it in a layer declared before `cv.primitives`:
|
|
70
|
+
|
|
71
|
+
```css
|
|
72
|
+
@layer reset, cv;
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Box
|
|
76
|
+
|
|
77
|
+
`Box` renders any HTML tag through `as`, a `div` by default, and is styled
|
|
78
|
+
through props. Native attributes, events and `ref` follow the rendered tag.
|
|
79
|
+
With `asChild`, it renders no element and styles its single child instead
|
|
80
|
+
(see [`asChild`](#aschild)). Tags of pure text (headings, `p` and inline tags such as `strong`, `em` or
|
|
81
|
+
`code`) are not accepted: they belong to `Typography`. Tags that commonly
|
|
82
|
+
wrap other elements (`span`, `a`, `li`, `label`, `dt`, `dd`, `legend`,
|
|
83
|
+
`caption`, `figcaption`, `blockquote`, `pre`) are accepted by both.
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
import { vars } from '@choco-vanille/theme';
|
|
87
|
+
import { Box } from '@choco-vanille/primitives';
|
|
88
|
+
|
|
89
|
+
<Box
|
|
90
|
+
as={'section'}
|
|
91
|
+
backgroundColor={vars.color.surface.main}
|
|
92
|
+
borderRadius={'xl'}
|
|
93
|
+
display={'flex'}
|
|
94
|
+
gap={8}
|
|
95
|
+
padding={16}
|
|
96
|
+
>
|
|
97
|
+
...
|
|
98
|
+
</Box>;
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
- Layout props (spacing, flex, grid, position, overflow, borders, shadow,
|
|
102
|
+
opacity) take a value among theme tokens and CSS keywords; any
|
|
103
|
+
other value is a type error. They resolve to static classes: no style is
|
|
104
|
+
computed at runtime. A value outside the set that reaches a component
|
|
105
|
+
anyway, from untyped data for instance, is ignored: it adds no class, and
|
|
106
|
+
outside production builds (`process.env.NODE_ENV` other than
|
|
107
|
+
`production`, or no `process` at all) an error naming the prop, the value
|
|
108
|
+
and the possible values is logged to the console, without interrupting
|
|
109
|
+
the render.
|
|
110
|
+
- Axis and side shorthands (`padding`, `paddingX`, `paddingY`, `margin`,
|
|
111
|
+
`marginX`, `marginY`, `inset`, `insetX`, `insetY`, `gap`, `overflow`,
|
|
112
|
+
`borderWidth`, `borderRadius`) are overridden by more specific props:
|
|
113
|
+
`paddingTop` over `paddingY` over `padding`.
|
|
114
|
+
- Sizing props (`width`, `minWidth`, `maxWidth`, `height`, `minHeight`,
|
|
115
|
+
`maxHeight`) accept any CSS value of their property, typed as in `style`:
|
|
116
|
+
a number is a length in pixels (`width={320}` is `320px`, `0` stays `0`),
|
|
117
|
+
and a string is used as given (`'50%'`, `'20rem'`, `'100dvh'`,
|
|
118
|
+
`'calc(100% - 2rem)'`). A prop that is set adds a class reading a private
|
|
119
|
+
CSS variable set inline; an absent prop adds neither.
|
|
120
|
+
- `color`, `backgroundColor` and `borderColor` accept any CSS value, such as
|
|
121
|
+
a theme variable.
|
|
122
|
+
- `scrollbar={'thin'}` styles the native scrollbars of a scroll container:
|
|
123
|
+
thin, without arrow buttons, with a rounded thumb in the scrollbar colors
|
|
124
|
+
of the theme, `vars.color.scrollbar.thumb` on `vars.color.scrollbar.track`,
|
|
125
|
+
and `thumbHover` under the pointer in Chromium and Safari. Chromium and
|
|
126
|
+
Safari get `::-webkit-scrollbar` pseudo-elements only, since
|
|
127
|
+
`scrollbar-width` or `scrollbar-color` would bring the arrows of Chromium
|
|
128
|
+
back; Firefox gets `scrollbar-width: thin` and `scrollbar-color`. In forced
|
|
129
|
+
colors, the scrollbars keep the colors of the system. It makes nothing
|
|
130
|
+
scroll: pair it with an `overflow` prop on the same element. Its class sits
|
|
131
|
+
in the `cv.primitives.composites` layer. The library sets no scrollbar
|
|
132
|
+
style on any other element: apply it to each container you want styled.
|
|
133
|
+
- Style props replace the native attributes of the same name (`color`,
|
|
134
|
+
`width`, `height`); `className` and `style` are merged with the generated
|
|
135
|
+
ones, and your `style` prevails over the sizing and color props.
|
|
136
|
+
|
|
137
|
+
### Flex and Grid
|
|
138
|
+
|
|
139
|
+
`Flex` and `Grid` are boxes laid out as a flex container (in a row) and as a
|
|
140
|
+
grid container by default. They render the same tags as `Box`, take the same
|
|
141
|
+
style props, reuse the same classes and accept [`asChild`](#aschild) the same
|
|
142
|
+
way, their default `display` then applying to the child.
|
|
143
|
+
|
|
144
|
+
```tsx
|
|
145
|
+
import { Box, Flex, Grid } from '@choco-vanille/primitives';
|
|
146
|
+
|
|
147
|
+
<Flex alignItems={'center'} gap={16}>
|
|
148
|
+
<Box flex={1}>...</Box>
|
|
149
|
+
...
|
|
150
|
+
</Flex>;
|
|
151
|
+
<Grid gap={16} gridTemplateColumns={3}>
|
|
152
|
+
<Box gridColumn={'span 2'}>...</Box>
|
|
153
|
+
...
|
|
154
|
+
</Grid>;
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
- Only the default `display` differs from `Box`: `flex` on `Flex`, `grid`
|
|
158
|
+
on `Grid`. Every value of `display` stays accepted and replaces the
|
|
159
|
+
default, for instance `display={'none'}` to hide the container.
|
|
160
|
+
- Both take every style prop of `Box`: the props of grid containers on a
|
|
161
|
+
`Flex` and those of flex containers on a `Grid` apply once `display`
|
|
162
|
+
sets that layout, as on `<Flex display={'grid'} gridTemplateColumns={2}>`.
|
|
163
|
+
`FlexProps` and `GridProps` are those of `Box`.
|
|
164
|
+
- Item props (`flex`, `alignSelf`, `gridColumn`...) stay available on both,
|
|
165
|
+
since each can be the child of the other.
|
|
166
|
+
|
|
167
|
+
### Separator
|
|
168
|
+
|
|
169
|
+
`Separator` draws a line between groups of content, `1px` thick, in the
|
|
170
|
+
border color of the theme (`vars.color.border.main`) by default, without
|
|
171
|
+
margins: space it with the margin props or with the `gap` of its container.
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
import { Flex, Separator, Typography } from '@choco-vanille/primitives';
|
|
175
|
+
|
|
176
|
+
<Separator marginY={24} />;
|
|
177
|
+
<Flex alignItems={'center'} gap={12}>
|
|
178
|
+
<Typography as={'span'}>Draft</Typography>
|
|
179
|
+
<Separator orientation={'vertical'} />
|
|
180
|
+
<Typography as={'span'}>3 comments</Typography>
|
|
181
|
+
</Flex>;
|
|
182
|
+
<Flex aria-label={'Resources'} as={'nav'} flexDirection={'column'} gap={8}>
|
|
183
|
+
...
|
|
184
|
+
<Separator decorative={false} />
|
|
185
|
+
...
|
|
186
|
+
</Flex>;
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
- Horizontal by default, it renders an `hr`, whose default margins and
|
|
190
|
+
borders are reset, and which stretches along the width of a flex column
|
|
191
|
+
even when its items are aligned otherwise. `orientation={'vertical'}`
|
|
192
|
+
renders a `div` that stretches along the height of the flex row holding
|
|
193
|
+
it; outside a flex row, give it a `height`. `alignSelf` overrides both.
|
|
194
|
+
- It is decorative by default (`decorative` is `true` when absent): it only
|
|
195
|
+
draws the line, and `role="none"` and `aria-hidden="true"` hide it from
|
|
196
|
+
assistive technologies, without `aria-orientation`.
|
|
197
|
+
- `decorative={false}` announces it as a separator: the `hr` keeps its
|
|
198
|
+
implicit role, and the vertical `div` gets `role="separator"` and
|
|
199
|
+
`aria-orientation="vertical"`. Use it when the line splits groups of
|
|
200
|
+
content or of menu items that screen readers must tell apart, such as two
|
|
201
|
+
groups of links in a navigation; keep the default when the line only
|
|
202
|
+
shows a split the structure of the content already conveys, such as
|
|
203
|
+
between paragraphs, headed sections or items of a row.
|
|
204
|
+
- Style props: the margin, sizing (`width`, `height`...), self-alignment
|
|
205
|
+
(`alignSelf`, `justifySelf`) and grid item (`gridColumn`, `gridRow`) props
|
|
206
|
+
of `Box`, with the same values and classes, the sizing props taking any
|
|
207
|
+
CSS value, and `color`, which colors the line with any CSS value. `SeparatorStyleProps` types them. The line
|
|
208
|
+
is drawn in the current color: a class of yours setting `color` colors it
|
|
209
|
+
too. The default line sits in the `cv.primitives.base` layer, so these
|
|
210
|
+
props and your unlayered classes override it.
|
|
211
|
+
- Other props (attributes, events, `ref`) are forwarded to the element; the
|
|
212
|
+
role and the ARIA attributes always follow `orientation` and `decorative`.
|
|
213
|
+
There is no `as` nor `asChild`: the element and its role follow the
|
|
214
|
+
orientation, and a separator has no content a child could provide.
|
|
215
|
+
|
|
216
|
+
### Typography
|
|
217
|
+
|
|
218
|
+
`Typography` renders any text tag through `as`, a `p` by default, with the
|
|
219
|
+
default style of that tag, built from the theme tokens. The style follows the
|
|
220
|
+
tag: there is no variant. Native attributes, events and `ref` follow the
|
|
221
|
+
rendered tag. With [`asChild`](#aschild), it styles its single child instead.
|
|
222
|
+
|
|
223
|
+
```tsx
|
|
224
|
+
import { Typography } from '@choco-vanille/primitives';
|
|
225
|
+
|
|
226
|
+
<Typography as={'h1'}>Title</Typography>;
|
|
227
|
+
<Typography marginTop={8} maxLines={2} textAlign={'center'}>
|
|
228
|
+
...
|
|
229
|
+
</Typography>;
|
|
230
|
+
<Typography as={'a'} href={'/docs'}>
|
|
231
|
+
Documentation
|
|
232
|
+
</Typography>;
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
- Accepted tags: `h1` to `h6`, `p`, `blockquote`, `pre`, `figcaption`;
|
|
236
|
+
`span`, `strong`, `b`, `em`, `i`, `small`, `mark`, `code`, `kbd`,
|
|
237
|
+
`samp`, `abbr`, `cite`, `q`, `del`, `ins`, `sub`, `sup`, `time`, `u`,
|
|
238
|
+
`s`; `a`, `label`, `li`, `dt`, `dd`, `legend`, `caption`.
|
|
239
|
+
- Headings, paragraphs and other block tags set their font, size, weight,
|
|
240
|
+
line height and color, and reset their margins: space them with props or
|
|
241
|
+
with the `gap` of their container. Inline tags inherit from their sentence
|
|
242
|
+
and only add what defines them. Links are underlined and show a focus ring.
|
|
243
|
+
- Style props override the default style of the tag: `fontFamily`,
|
|
244
|
+
`fontSize`, `fontWeight`, `fontStyle`, `lineHeight`, `letterSpacing`,
|
|
245
|
+
`textAlign`, `textTransform`, `textDecoration`, `textWrap`, `whiteSpace`,
|
|
246
|
+
`wordBreak`, `overflowWrap` and `maxLines`, with theme tokens and CSS
|
|
247
|
+
keywords as values, and `color`, which accepts any CSS value.
|
|
248
|
+
- The spacing, sizing, `display`, overflow (`scrollbar` included), flex item,
|
|
249
|
+
grid item and opacity props of `Box` are available too, with the same
|
|
250
|
+
values: tokens, and any CSS value for the sizing props.
|
|
251
|
+
- `maxLines` (`1` to `6`) truncates the text after a number of lines, with
|
|
252
|
+
an ellipsis, and turns the element into a block that shrinks inside a flex
|
|
253
|
+
or grid container. `1` keeps the text on a single line, aligned to the
|
|
254
|
+
start whatever `textAlign`, and cuts it where it overflows; `2` to `6`
|
|
255
|
+
clamp it after that many lines and let a word longer than the line break
|
|
256
|
+
anywhere. The full text stays available to assistive technologies: give
|
|
257
|
+
sighted users another way to read it.
|
|
258
|
+
- With `asChild`, no tag style applies, since the child sets the element:
|
|
259
|
+
only the style props do. A link rendered this way, for instance, gets
|
|
260
|
+
neither the link colors nor the focus ring of `<Typography as={'a'}>`:
|
|
261
|
+
keep its focus visible through your own styles.
|
|
262
|
+
|
|
263
|
+
### `asChild`
|
|
264
|
+
|
|
265
|
+
`Box`, `Flex`, `Grid`, `Typography` and `VisuallyHidden` render
|
|
266
|
+
their own element, whose tag `as` chooses. With `asChild`, they render none: their single child renders
|
|
267
|
+
the element, with their classes, styles, other props and `ref` merged onto
|
|
268
|
+
it. Use it when another component must render the element, such as the
|
|
269
|
+
`Link` of a router, without a wrapping `div`. The **Guides / asChild** page
|
|
270
|
+
of the Storybook walks through it with live examples and the HTML they
|
|
271
|
+
render.
|
|
272
|
+
|
|
273
|
+
```tsx
|
|
274
|
+
import { Box, Typography } from '@choco-vanille/primitives';
|
|
275
|
+
import { Link } from 'react-router';
|
|
276
|
+
|
|
277
|
+
// Renders a single `a`, from the router link, with the classes of the Box.
|
|
278
|
+
<Box asChild borderRadius={'lg'} paddingX={16} paddingY={8}>
|
|
279
|
+
<Link to={'/docs'}>Documentation</Link>
|
|
280
|
+
</Box>;
|
|
281
|
+
<Typography asChild fontWeight={'bold'}>
|
|
282
|
+
<Link to={'/pricing'}>Pricing</Link>
|
|
283
|
+
</Typography>;
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
- The child keeps its type, tag, props and children. A component child must
|
|
287
|
+
forward `className`, `style`, `ref`, event handlers and other props to its
|
|
288
|
+
element, as router links do.
|
|
289
|
+
- Classes are joined: the generated ones and the `className` of the
|
|
290
|
+
primitive, then the `className` of the child. Empty classes add nothing.
|
|
291
|
+
- Styles are merged: the `style` of the primitive, color variables included,
|
|
292
|
+
then the `style` of the child, whose value wins for a property both set.
|
|
293
|
+
- A child `className` or `style` given as a function of its state, as on
|
|
294
|
+
the `NavLink` of a router, stays a function: the child calls it with its
|
|
295
|
+
state, and it returns the classes of the primitive followed by those the
|
|
296
|
+
child's function returns, or the merged styles. A function returning no
|
|
297
|
+
class or no style only drops its own side.
|
|
298
|
+
- Event handlers (props starting with `on` and a capital letter): when both
|
|
299
|
+
give one, the child's runs first, then the primitive's, with the same
|
|
300
|
+
event, even when the child's called `event.preventDefault()`.
|
|
301
|
+
- Refs are combined: both receive the element and are released with it. The
|
|
302
|
+
combined ref keeps its identity while both refs stay the same.
|
|
303
|
+
- Any other prop given to both keeps the child's value. Style props reach the
|
|
304
|
+
child as classes and color variables, never as attributes.
|
|
305
|
+
- `as` and `asChild` are exclusive: combining them is a type error. With
|
|
306
|
+
`asChild`, `children` is a single React element, and attributes, events
|
|
307
|
+
and `ref` are typed for any `HTMLElement`. `BoxAsChildProps`,
|
|
308
|
+
`FlexAsChildProps`, `GridAsChildProps`, `TypographyAsChildProps` and
|
|
309
|
+
`VisuallyHiddenAsChildProps` type this form; `BoxProps`, `FlexProps`,
|
|
310
|
+
`GridProps`, `TypographyProps` and `VisuallyHiddenProps` the element form,
|
|
311
|
+
where `asChild` is `false` or absent.
|
|
312
|
+
- Children that are not a single React element able to receive props (text,
|
|
313
|
+
a number, several elements, a fragment, `null`) make rendering throw an
|
|
314
|
+
`Error`, caught by the nearest error boundary. A fragment passes the type
|
|
315
|
+
check: only rendering rejects it.
|
|
316
|
+
|
|
317
|
+
### Icon
|
|
318
|
+
|
|
319
|
+
`Icon` displays trusted local Iconify data as an SVG using
|
|
320
|
+
`@iconify/react/offline`, with a font size token, CSS sizes and CSS colors.
|
|
321
|
+
Icons are decorative unless given an accessible name. The package ships no icon set:
|
|
322
|
+
keep your own registry of icon data and pass its entries through `icon`.
|
|
323
|
+
|
|
324
|
+
```tsx
|
|
325
|
+
import { vars } from '@choco-vanille/theme';
|
|
326
|
+
import type { IconData } from '@choco-vanille/primitives';
|
|
327
|
+
import { Icon } from '@choco-vanille/primitives';
|
|
328
|
+
|
|
329
|
+
const appIcons = {
|
|
330
|
+
check: {
|
|
331
|
+
body: '<path fill="none" stroke="currentColor" stroke-width="2" d="M20 6 9 17l-5-5"/>',
|
|
332
|
+
width: 24,
|
|
333
|
+
height: 24,
|
|
334
|
+
},
|
|
335
|
+
} satisfies Record<string, IconData>;
|
|
336
|
+
|
|
337
|
+
<Icon icon={appIcons.check} size={20} />;
|
|
338
|
+
<Icon
|
|
339
|
+
aria-label={'Done'}
|
|
340
|
+
color={vars.color.success.text}
|
|
341
|
+
icon={appIcons.check}
|
|
342
|
+
/>;
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
- `IconData` is the Iconify icon format: an SVG `body` without the `svg`
|
|
346
|
+
element, and its `width` and `height` in viewBox units (16 when absent).
|
|
347
|
+
Icons are drawn from this data, never fetched. The body is inserted as
|
|
348
|
+
markup: only pass data from a trusted source, never user input.
|
|
349
|
+
- Without size props, an icon is `1em` high and follows the font size of its
|
|
350
|
+
text; its width follows the ratio of its data. `size` sets the font size
|
|
351
|
+
of the icon to a font size token of the theme (`IconSize`): `size={20}`
|
|
352
|
+
is `1.25rem`, and both axes scale with it. `width` and `height` take any CSS value, as on `Box`, and
|
|
353
|
+
replace the default size on their axis, `size` included; the other axis
|
|
354
|
+
keeps its default, computed from the font size. `100%` fills the
|
|
355
|
+
container; `auto` and the content keywords come back to the default size.
|
|
356
|
+
The drawing keeps its ratio, centered in the box.
|
|
357
|
+
- Icons draw with `currentColor`: they inherit the text color, and `color`
|
|
358
|
+
accepts any CSS value.
|
|
359
|
+
- Without a name, an icon is hidden from assistive technologies
|
|
360
|
+
(`aria-hidden="true"`), without role and not focusable. A non-empty
|
|
361
|
+
`aria-label` or `aria-labelledby` makes it an image (`role="img"`)
|
|
362
|
+
announced by that name. Inside a control, name the control instead, with
|
|
363
|
+
an `aria-label`, and keep the icon decorative:
|
|
364
|
+
|
|
365
|
+
```tsx
|
|
366
|
+
<button aria-label={'Close'} type={'button'}>
|
|
367
|
+
<Icon icon={appIcons.close} />
|
|
368
|
+
</button>
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
When the name must be real text, for instance so that the translation
|
|
372
|
+
built into browsers, which does not always translate `aria-label`,
|
|
373
|
+
reaches it, put it in a [`VisuallyHidden`](#visuallyhidden) next to the
|
|
374
|
+
icon instead: `<VisuallyHidden>Close</VisuallyHidden>`.
|
|
375
|
+
|
|
376
|
+
- Every style prop of `Box` applies to the `svg` box, with the same values,
|
|
377
|
+
except those of flex and grid containers (`gap`, `rowGap`, `columnGap`,
|
|
378
|
+
`flexDirection`, `flexWrap`, `alignItems`, `alignContent`,
|
|
379
|
+
`justifyContent`, `justifyItems` and the `grid*` templates and tracks),
|
|
380
|
+
which have no effect on the drawing. `backgroundColor`, `borderColor`,
|
|
381
|
+
borders, radii, padding and shadows draw a badge around the icon:
|
|
382
|
+
|
|
383
|
+
```tsx
|
|
384
|
+
<Icon
|
|
385
|
+
backgroundColor={vars.color.success.subtle}
|
|
386
|
+
borderRadius={'full'}
|
|
387
|
+
color={vars.color.success.subtleForeground}
|
|
388
|
+
icon={appIcons.check}
|
|
389
|
+
padding={8}
|
|
390
|
+
size={24}
|
|
391
|
+
/>
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
The box is `content-box`: padding and borders add to the size (the badge
|
|
395
|
+
above is 40px wide around a 24px drawing). With `boxSizing={'border-box'}`
|
|
396
|
+
they shrink the drawing instead. Rounded corners do not clip the drawing
|
|
397
|
+
unless `overflow` is set.
|
|
398
|
+
|
|
399
|
+
- Other props (attributes, events, `ref`) are forwarded to the `svg`
|
|
400
|
+
element.
|
|
401
|
+
|
|
402
|
+
### VisuallyHidden
|
|
403
|
+
|
|
404
|
+
`VisuallyHidden` hides its content from the screen and keeps it in the
|
|
405
|
+
accessibility tree, so screen readers still announce it. It renders any HTML
|
|
406
|
+
tag through `as`, a `span` by default, and accepts [`asChild`](#aschild).
|
|
407
|
+
|
|
408
|
+
```tsx
|
|
409
|
+
import { VisuallyHidden } from '@choco-vanille/primitives';
|
|
410
|
+
|
|
411
|
+
<VisuallyHidden as={'a'} focusable href={'#content'}>
|
|
412
|
+
Skip to content
|
|
413
|
+
</VisuallyHidden>;
|
|
414
|
+
<a href={'/pricing'}>
|
|
415
|
+
Read more
|
|
416
|
+
<VisuallyHidden> about pricing</VisuallyHidden>
|
|
417
|
+
</a>;
|
|
418
|
+
<VisuallyHidden as={'h2'}>Filters</VisuallyHidden>;
|
|
419
|
+
<label>
|
|
420
|
+
<VisuallyHidden>Search the documentation</VisuallyHidden>
|
|
421
|
+
<input type={'search'} />
|
|
422
|
+
</label>;
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
- Use it where text must exist for screen readers without showing: a skip
|
|
426
|
+
link, shown on focus with `focusable`; the end of a visible text, such as
|
|
427
|
+
"about pricing" after "Read more", which an `aria-label` could not add
|
|
428
|
+
since it replaces the visible text; a heading that structures a page
|
|
429
|
+
without being displayed; the label of a field whose purpose the layout
|
|
430
|
+
already shows. An `aria-label` is not read reliably on an element without
|
|
431
|
+
a role, such as a `span` or a `div`: hidden text is.
|
|
432
|
+
- To name a button that only shows an icon, an `aria-label` on the button
|
|
433
|
+
is enough (see [Icon](#icon)). Content no one should perceive belongs to
|
|
434
|
+
`hidden` instead, and content screen readers should skip to
|
|
435
|
+
`aria-hidden`.
|
|
436
|
+
- The element leaves the flow (`position: absolute`), shrinks to one pixel,
|
|
437
|
+
is clipped to nothing and stays on one line. Unlike `display: none` or
|
|
438
|
+
`visibility: hidden`, this keeps it in the accessibility tree. These
|
|
439
|
+
styles sit in the `cv.primitives.base` layer: an unlayered style of yours
|
|
440
|
+
on the element overrides them, so leave its position, size, spacing,
|
|
441
|
+
border, overflow, clipping and white space to the component.
|
|
442
|
+
- `focusable` shows the element again while it or one of its descendants
|
|
443
|
+
has the focus (`:focus`, `:focus-within`), with the styles its own classes
|
|
444
|
+
give it: a skip link appears when reached with the keyboard. It does not
|
|
445
|
+
make the element focusable: render a link or a button, or wrap one.
|
|
446
|
+
- It takes no style props. Other props (attributes, events, `ref`) follow
|
|
447
|
+
the rendered tag; `className` comes after the hiding class, and `style` is
|
|
448
|
+
kept as given.
|
|
449
|
+
- With `asChild`, the single child is hidden instead, such as a router link
|
|
450
|
+
used as a skip link, or a `Typography` link, which then keeps its link
|
|
451
|
+
style and focus ring when shown:
|
|
452
|
+
|
|
453
|
+
```tsx
|
|
454
|
+
<VisuallyHidden asChild focusable>
|
|
455
|
+
<Typography as={'a'} href={'#content'}>
|
|
456
|
+
Skip to content
|
|
457
|
+
</Typography>
|
|
458
|
+
</VisuallyHidden>
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
The style props of a child primitive sit in a later layer than the hiding
|
|
462
|
+
styles: give it none that sizes, spaces or positions it.
|
|
463
|
+
|
|
464
|
+
## Development
|
|
465
|
+
|
|
466
|
+
The package lives in the choco-vanille monorepo: install the dependencies
|
|
467
|
+
and format the code from the repository root, as described in its README.
|
|
468
|
+
It reads the built `@choco-vanille/theme` of the workspace: build the theme
|
|
469
|
+
before running Storybook, the type checks or the tests.
|
|
470
|
+
|
|
471
|
+
| Script | Description |
|
|
472
|
+
| ------------------------ | ---------------------------------------------------- |
|
|
473
|
+
| `pnpm dev` | Starts Storybook on port 6007. |
|
|
474
|
+
| `pnpm build` | Builds the library into `dist`. |
|
|
475
|
+
| `pnpm build:storybook` | Builds the static Storybook into `storybook-static`. |
|
|
476
|
+
| `pnpm lint` | Lints the sources with ESLint. |
|
|
477
|
+
| `pnpm typecheck` | Type-checks the project. |
|
|
478
|
+
| `pnpm test` | Runs the unit tests with Vitest. |
|
|
479
|
+
| `pnpm test:distribution` | Checks the built package (run `pnpm build` first). |
|
|
480
|
+
|
|
481
|
+
## License
|
|
482
|
+
|
|
483
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Slot.cjs","names":["useSlot","cloneElement","require_useSlot_hook","react","Slot","mergedProps","props","element"],"sources":["../../../../../src/components/_common/components/slot/Slot.tsx"],"sourcesContent":["import type { FC } from 'react';\nimport { cloneElement } from 'react';\n\nimport { useSlot } from '@primitives/components/_common/components/slot/hooks/use-slot/useSlot.hook';\nimport type { SlotProps } from '@primitives/components/_common/components/slot/types';\n\n/**\n * Renders the single child of a component used with `asChild` in place of\n * the component's own element, with the props of the component merged onto\n * it: classes joined, styles merged with the child's prevailing, event\n * handlers both called (the child's first), refs composed, and the child's\n * value kept for any other prop given on both sides.\n *\n * @param props Props of the component, its single child included.\n * @param props.children Single element to render, such as a router link.\n * @returns The child, cloned with the merged props.\n * @throws {Error} When the children are not a single element.\n */\nconst Slot: FC<SlotProps> = (props) => {\n const { element, props: mergedProps } = useSlot(props);\n\n return cloneElement(element, mergedProps);\n};\n\nexport default Slot;\n"],"mappings":"A,4EAkBA,IAAMI,EAAuB,GAAU,CACnC,GAAM,CAAE,UAAS,MAAOC,GAAgBL,EAAAA,QAAQM,CAAK,EAErD,OAAA,EAAOL,EAAAA,aAAAA,CAAaM,EAASF,CAAW,CAC5C,E"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useSlot as e } from "./hooks/use-slot/useSlot.hook.js";
|
|
2
|
+
import { cloneElement as t } from "react";
|
|
3
|
+
// src/components/_common/components/slot/Slot.tsx
|
|
4
|
+
var n = (n) => {
|
|
5
|
+
let { element: r, props: i } = e(n);
|
|
6
|
+
return t(r, i);
|
|
7
|
+
};
|
|
8
|
+
export { n as default };
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=Slot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Slot.js","names":["Slot","mergedProps","useSlot","props","cloneElement","element"],"sources":["../../../../../src/components/_common/components/slot/Slot.tsx"],"sourcesContent":["import type { FC } from 'react';\nimport { cloneElement } from 'react';\n\nimport { useSlot } from '@primitives/components/_common/components/slot/hooks/use-slot/useSlot.hook';\nimport type { SlotProps } from '@primitives/components/_common/components/slot/types';\n\n/**\n * Renders the single child of a component used with `asChild` in place of\n * the component's own element, with the props of the component merged onto\n * it: classes joined, styles merged with the child's prevailing, event\n * handlers both called (the child's first), refs composed, and the child's\n * value kept for any other prop given on both sides.\n *\n * @param props Props of the component, its single child included.\n * @param props.children Single element to render, such as a router link.\n * @returns The child, cloned with the merged props.\n * @throws {Error} When the children are not a single element.\n */\nconst Slot: FC<SlotProps> = (props) => {\n const { element, props: mergedProps } = useSlot(props);\n\n return cloneElement(element, mergedProps);\n};\n\nexport default Slot;\n"],"mappings":";;;AAkBA,IAAMA,KAAuB,MAAU;CACnC,IAAM,EAAE,YAAS,OAAOC,MAAgBC,EAAQC,CAAK;CAErD,OAAOC,EAAaC,GAASJ,CAAW;AAC5C"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const e=require("../../utils/composeRefs.utils.cjs"),t=require("../../utils/getSlotChild.utils.cjs"),n=require("../../utils/mergeSlotProps.utils.cjs");let r=require("react");var i=i=>{let{children:a,ref:o,...s}=i,c=t.getSlotChild(a),l=n.mergeSlotProps(s,c.props),u=c.props.ref,d=(0,r.useMemo)(()=>e.composeRefs(o,u),[o,u]);return{element:c,props:d===void 0?l:{...l,ref:d}}};exports.useSlot=i;
|
|
2
|
+
//# sourceMappingURL=useSlot.hook.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSlot.hook.cjs","names":["getSlotChild","mergeSlotProps","useMemo","composeRefs","require_composeRefs_utils","require_getSlotChild_utils","require_mergeSlotProps_utils","react","useSlot","slotRef","slotProps","props","element","children","mergedProps","childRef","ref"],"sources":["../../../../../../../src/components/_common/components/slot/hooks/use-slot/useSlot.hook.ts"],"sourcesContent":["import { useMemo } from 'react';\n\nimport type { UseSlotResult } from '@primitives/components/_common/components/slot/hooks/use-slot/types';\nimport type { SlotProps } from '@primitives/components/_common/components/slot/types';\nimport { composeRefs } from '@primitives/components/_common/components/slot/utils/composeRefs.utils';\nimport { getSlotChild } from '@primitives/components/_common/components/slot/utils/getSlotChild.utils';\nimport { mergeSlotProps } from '@primitives/components/_common/components/slot/utils/mergeSlotProps.utils';\n\n/**\n * Resolves what a `Slot` renders: its single child, with the props of the\n * slot merged onto its own and both refs composed. The composed ref keeps\n * its identity while both refs do, so the node is not detached and attached\n * again on every render.\n *\n * @param props Props of the slot.\n * @param props.children Single element to render in place of the slot.\n * @param props.ref Ref of the slot, composed with the ref of the child.\n * @returns The child element and the props to clone it with.\n * @throws {Error} When the children are not a single element.\n */\nexport const useSlot = (props: SlotProps): UseSlotResult => {\n const { children, ref: slotRef, ...slotProps } = props;\n const element = getSlotChild(children);\n\n // Merged first: the child's props go nowhere else, so its ref cannot change\n // after being read and the composed ref keeps its identity.\n const mergedProps = mergeSlotProps(slotProps, element.props);\n const childRef = element.props.ref;\n const ref = useMemo(\n () => composeRefs(slotRef, childRef),\n [slotRef, childRef],\n );\n\n return {\n element,\n props: ref === undefined ? mergedProps : { ...mergedProps, ref },\n };\n};\n"],"mappings":"A,8KAoBA,IAAaQ,EAAW,GAAoC,CACxD,GAAM,CAAE,WAAU,IAAKC,EAAS,GAAGC,GAAcC,EAC3CC,EAAUZ,EAAAA,aAAaa,CAAQ,EAI/BC,EAAcb,EAAAA,eAAeS,EAAWE,EAAQ,KAAK,EACrDG,EAAWH,EAAQ,MAAM,IACzBI,GAAA,EAAMd,EAAAA,QAAAA,KACFC,EAAAA,YAAYM,EAASM,CAAQ,EACnC,CAACN,EAASM,CAAQ,CACtB,EAEA,MAAO,CACH,UACA,MAAOC,IAAQ,IAAA,GAAYF,EAAc,CAAE,GAAGA,EAAa,KAAI,CACnE,CACJ,E"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { composeRefs as e } from "../../utils/composeRefs.utils.js";
|
|
2
|
+
import { getSlotChild as t } from "../../utils/getSlotChild.utils.js";
|
|
3
|
+
import { mergeSlotProps as n } from "../../utils/mergeSlotProps.utils.js";
|
|
4
|
+
import { useMemo as r } from "react";
|
|
5
|
+
// src/components/_common/components/slot/hooks/use-slot/useSlot.hook.ts
|
|
6
|
+
var i = (i) => {
|
|
7
|
+
let { children: a, ref: o, ...s } = i, c = t(a), l = n(s, c.props), u = c.props.ref, d = r(() => e(o, u), [o, u]);
|
|
8
|
+
return {
|
|
9
|
+
element: c,
|
|
10
|
+
props: d === void 0 ? l : {
|
|
11
|
+
...l,
|
|
12
|
+
ref: d
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export { i as useSlot };
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=useSlot.hook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSlot.hook.js","names":["useSlot","slotRef","slotProps","props","element","getSlotChild","children","mergedProps","mergeSlotProps","childRef","ref","useMemo","composeRefs"],"sources":["../../../../../../../src/components/_common/components/slot/hooks/use-slot/useSlot.hook.ts"],"sourcesContent":["import { useMemo } from 'react';\n\nimport type { UseSlotResult } from '@primitives/components/_common/components/slot/hooks/use-slot/types';\nimport type { SlotProps } from '@primitives/components/_common/components/slot/types';\nimport { composeRefs } from '@primitives/components/_common/components/slot/utils/composeRefs.utils';\nimport { getSlotChild } from '@primitives/components/_common/components/slot/utils/getSlotChild.utils';\nimport { mergeSlotProps } from '@primitives/components/_common/components/slot/utils/mergeSlotProps.utils';\n\n/**\n * Resolves what a `Slot` renders: its single child, with the props of the\n * slot merged onto its own and both refs composed. The composed ref keeps\n * its identity while both refs do, so the node is not detached and attached\n * again on every render.\n *\n * @param props Props of the slot.\n * @param props.children Single element to render in place of the slot.\n * @param props.ref Ref of the slot, composed with the ref of the child.\n * @returns The child element and the props to clone it with.\n * @throws {Error} When the children are not a single element.\n */\nexport const useSlot = (props: SlotProps): UseSlotResult => {\n const { children, ref: slotRef, ...slotProps } = props;\n const element = getSlotChild(children);\n\n // Merged first: the child's props go nowhere else, so its ref cannot change\n // after being read and the composed ref keeps its identity.\n const mergedProps = mergeSlotProps(slotProps, element.props);\n const childRef = element.props.ref;\n const ref = useMemo(\n () => composeRefs(slotRef, childRef),\n [slotRef, childRef],\n );\n\n return {\n element,\n props: ref === undefined ? mergedProps : { ...mergedProps, ref },\n };\n};\n"],"mappings":";;;;;AAoBA,IAAaA,KAAW,MAAoC;CACxD,IAAM,EAAE,aAAU,KAAKC,GAAS,GAAGC,MAAcC,GAC3CC,IAAUC,EAAaC,CAAQ,GAI/BC,IAAcC,EAAeN,GAAWE,EAAQ,KAAK,GACrDK,IAAWL,EAAQ,MAAM,KACzBM,IAAMC,QACFC,EAAYX,GAASQ,CAAQ,GACnC,CAACR,GAASQ,CAAQ,CACtB;CAEA,OAAO;EACH;EACA,OAAOC,MAAQ,KAAA,IAAYH,IAAc;GAAE,GAAGA;GAAa;EAAI;CACnE;AACJ"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var e=(e,t)=>{if(typeof e==`function`){let n=e(t);return typeof n==`function`?()=>{n()}:()=>{e(null)}}if(typeof e==`object`&&e&&`current`in e)return e.current=t,()=>{e.current=null};throw Error(`A ref must be a function or an object holding \`current\`, not ${String(e)}.`)};exports.attachRef=e;
|
|
2
|
+
//# sourceMappingURL=attachRef.utils.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachRef.utils.cjs","names":["attachRef","ref","cleanup","node"],"sources":["../../../../../../src/components/_common/components/slot/utils/attachRef.utils.ts"],"sourcesContent":["/**\n * Attaches a node to a ref, callback or object, and tells how to detach it:\n * the cleanup a callback ref returns, otherwise a call with `null` or an\n * object ref reset to `null`.\n *\n * @param ref Callback or object ref.\n * @param node Node to attach.\n * @returns The function detaching the node.\n * @throws {Error} When the ref is neither a function nor an object holding\n * `current`.\n */\nexport const attachRef = (ref: unknown, node: unknown): (() => void) => {\n if (typeof ref === 'function') {\n const cleanup: unknown = ref(node);\n\n return typeof cleanup === 'function'\n ? (): void => {\n cleanup();\n }\n : (): void => {\n ref(null);\n };\n }\n\n if (typeof ref === 'object' && ref !== null && 'current' in ref) {\n ref.current = node;\n\n return (): void => {\n ref.current = null;\n };\n }\n\n throw new Error(\n `A ref must be a function or an object holding \\`current\\`, not ${String(ref)}.`,\n );\n};\n"],"mappings":"AAWA,IAAaA,GAAa,EAAc,IAAgC,CACpE,GAAI,OAAOC,GAAQ,WAAY,CAC3B,IAAMC,EAAmBD,EAAIE,CAAI,EAEjC,OAAO,OAAOD,GAAY,eACR,CACRA,EAAQ,CACZ,MACY,CACRD,EAAI,IAAI,CACZ,CACV,CAEA,GAAI,OAAOA,GAAQ,UAAYA,GAAgB,YAAaA,EAGxD,MAFA,GAAI,QAAUE,MAEK,CACf,EAAI,QAAU,IAClB,EAGJ,MAAU,MACN,kEAAkE,OAAOF,CAAG,EAAE,EAClF,CACJ,E"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/components/_common/components/slot/utils/attachRef.utils.ts
|
|
2
|
+
var e = (e, t) => {
|
|
3
|
+
if (typeof e == "function") {
|
|
4
|
+
let n = e(t);
|
|
5
|
+
return typeof n == "function" ? () => {
|
|
6
|
+
n();
|
|
7
|
+
} : () => {
|
|
8
|
+
e(null);
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
if (typeof e == "object" && e && "current" in e) return e.current = t, () => {
|
|
12
|
+
e.current = null;
|
|
13
|
+
};
|
|
14
|
+
throw Error(`A ref must be a function or an object holding \`current\`, not ${String(e)}.`);
|
|
15
|
+
};
|
|
16
|
+
export { e as attachRef };
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=attachRef.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachRef.utils.js","names":["attachRef","ref","cleanup","node"],"sources":["../../../../../../src/components/_common/components/slot/utils/attachRef.utils.ts"],"sourcesContent":["/**\n * Attaches a node to a ref, callback or object, and tells how to detach it:\n * the cleanup a callback ref returns, otherwise a call with `null` or an\n * object ref reset to `null`.\n *\n * @param ref Callback or object ref.\n * @param node Node to attach.\n * @returns The function detaching the node.\n * @throws {Error} When the ref is neither a function nor an object holding\n * `current`.\n */\nexport const attachRef = (ref: unknown, node: unknown): (() => void) => {\n if (typeof ref === 'function') {\n const cleanup: unknown = ref(node);\n\n return typeof cleanup === 'function'\n ? (): void => {\n cleanup();\n }\n : (): void => {\n ref(null);\n };\n }\n\n if (typeof ref === 'object' && ref !== null && 'current' in ref) {\n ref.current = node;\n\n return (): void => {\n ref.current = null;\n };\n }\n\n throw new Error(\n `A ref must be a function or an object holding \\`current\\`, not ${String(ref)}.`,\n );\n};\n"],"mappings":";AAWA,IAAaA,KAAa,GAAc,MAAgC;CACpE,IAAI,OAAOC,KAAQ,YAAY;EAC3B,IAAMC,IAAmBD,EAAIE,CAAI;EAEjC,OAAO,OAAOD,KAAY,mBACR;GACR,EAAQ;EACZ,UACY;GACR,EAAI,IAAI;EACZ;CACV;CAEA,IAAI,OAAOD,KAAQ,YAAYA,KAAgB,aAAaA,GAGxD,OAFA,EAAI,UAAUE,SAEK;EACf,EAAI,UAAU;CAClB;CAGJ,MAAU,MACN,kEAAkE,OAAOF,CAAG,EAAE,EAClF;AACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composeRefs.utils.cjs","names":["attachRef","require_attachRef_utils","composeRefs","slotRef","childRef","detachSlotRef","node","detachChildRef"],"sources":["../../../../../../src/components/_common/components/slot/utils/composeRefs.utils.ts"],"sourcesContent":["import { attachRef } from '@primitives/components/_common/components/slot/utils/attachRef.utils';\n\n/**\n * Combines the ref of a `Slot` with the ref of its child, so both receive\n * the rendered node: the slot's first, then the child's, and both are\n * detached when the node goes. Without a slot ref, the child keeps its own\n * ref; without a child ref, the slot ref is used as is.\n *\n * @param slotRef Ref given to the component rendered with `asChild`.\n * @param childRef Ref of the child element.\n * @returns The ref to clone the child with, a callback ref when both\n * exist; `undefined` to keep the child's own ref.\n */\nexport const composeRefs = (slotRef: unknown, childRef: unknown): unknown => {\n if (slotRef === undefined || slotRef === null) {\n return undefined;\n }\n\n if (childRef === undefined || childRef === null) {\n return slotRef;\n }\n\n return (node: unknown): (() => void) => {\n const detachSlotRef = attachRef(slotRef, node);\n const detachChildRef = attachRef(childRef, node);\n\n return (): void => {\n detachSlotRef();\n detachChildRef();\n };\n };\n};\n"],"mappings":"A,yCAaA,IAAaE,GAAe,EAAkB,IAA+B,CACzE,GAAIC,GAAqC,KAQzC,OAJIC,GAAuC,KAChCD,EAGH,GAAgC,CACpC,IAAME,EAAgBL,EAAAA,UAAUG,EAASG,CAAI,EACvCC,EAAiBP,EAAAA,UAAUI,EAAUE,CAAI,EAE/C,UAAmB,CACfD,EAAc,EACdE,EAAe,CACnB,CACJ,CACJ,E"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { attachRef as e } from "./attachRef.utils.js";
|
|
2
|
+
// src/components/_common/components/slot/utils/composeRefs.utils.ts
|
|
3
|
+
var t = (t, n) => {
|
|
4
|
+
if (t != null) return n == null ? t : (r) => {
|
|
5
|
+
let i = e(t, r), a = e(n, r);
|
|
6
|
+
return () => {
|
|
7
|
+
i(), a();
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export { t as composeRefs };
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=composeRefs.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composeRefs.utils.js","names":["composeRefs","slotRef","childRef","detachSlotRef","attachRef","node","detachChildRef"],"sources":["../../../../../../src/components/_common/components/slot/utils/composeRefs.utils.ts"],"sourcesContent":["import { attachRef } from '@primitives/components/_common/components/slot/utils/attachRef.utils';\n\n/**\n * Combines the ref of a `Slot` with the ref of its child, so both receive\n * the rendered node: the slot's first, then the child's, and both are\n * detached when the node goes. Without a slot ref, the child keeps its own\n * ref; without a child ref, the slot ref is used as is.\n *\n * @param slotRef Ref given to the component rendered with `asChild`.\n * @param childRef Ref of the child element.\n * @returns The ref to clone the child with, a callback ref when both\n * exist; `undefined` to keep the child's own ref.\n */\nexport const composeRefs = (slotRef: unknown, childRef: unknown): unknown => {\n if (slotRef === undefined || slotRef === null) {\n return undefined;\n }\n\n if (childRef === undefined || childRef === null) {\n return slotRef;\n }\n\n return (node: unknown): (() => void) => {\n const detachSlotRef = attachRef(slotRef, node);\n const detachChildRef = attachRef(childRef, node);\n\n return (): void => {\n detachSlotRef();\n detachChildRef();\n };\n };\n};\n"],"mappings":";;AAaA,IAAaA,KAAe,GAAkB,MAA+B;CACzE,IAAIC,KAAqC,MAQzC,OAJIC,KAAuC,OAChCD,KAGH,MAAgC;EACpC,IAAME,IAAgBC,EAAUH,GAASI,CAAI,GACvCC,IAAiBF,EAAUF,GAAUG,CAAI;EAE/C,aAAmB;GAEf,AADAF,EAAc,GACdG,EAAe;EACnB;CACJ;AACJ"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
let e=require("react");var t=t=>{if(!(0,e.isValidElement)(t)||t.type===e.Fragment)throw Error("`asChild` expects a single React element as child, such as a link: not a text, a list, a fragment or nothing.");return t};exports.getSlotChild=t;
|
|
2
|
+
//# sourceMappingURL=getSlotChild.utils.cjs.map
|