@vev/silke 1.1.3 → 1.1.6
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/CHANGELOG.md +26 -0
- package/dist/index.js +37 -35
- package/dist/index.js.LEGAL.txt +12 -0
- package/dist/styles.css +3673 -3509
- package/dist/types/components/index.d.ts +2 -0
- package/dist/types/components/silke-action-list/index.d.ts +1 -0
- package/dist/types/components/silke-action-list/silke-action-list.d.ts +11 -0
- package/dist/types/components/silke-autocomplete-field/autocomplete-types.d.ts +4 -0
- package/dist/types/components/silke-autocomplete-field/index.d.ts +1 -0
- package/dist/types/components/silke-autocomplete-field/silke-autocomplete-field-native.d.ts +60 -0
- package/dist/types/components/silke-autocomplete-field/silke-autocomplete-field.d.ts +3 -0
- package/dist/types/components/silke-autocomplete-field/silke-autocomplete-popup-native.d.ts +15 -0
- package/dist/types/components/silke-conditions-input/utils.d.ts +1 -1
- package/dist/types/components/silke-popover-native/index.d.ts +1 -0
- package/dist/types/components/silke-popover-native/silke-popover-native.d.ts +35 -0
- package/dist/types/components/silke-popover-native/types.d.ts +6 -0
- package/dist/types/components/silke-popover-native/use-popover-positioning.d.ts +9 -0
- package/dist/types/components/silke-select-field/index.d.ts +1 -0
- package/dist/types/components/silke-select-field/silke-select-field-native.d.ts +4 -0
- package/dist/types/icons.d.ts +2 -0
- package/package.json +3 -2
- package/src/components/index.ts +2 -0
- package/src/components/silke-action-list/index.ts +1 -0
- package/src/components/silke-action-list/silke-action-list.mdx +48 -0
- package/src/components/silke-action-list/silke-action-list.scss +21 -0
- package/src/components/silke-action-list/silke-action-list.tsx +85 -0
- package/src/components/silke-autocomplete-field/autocomplete-types.ts +4 -0
- package/src/components/silke-autocomplete-field/index.ts +1 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-field-native.tsx +346 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-field.mdx +39 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-field.tsx +3 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-popup-native.tsx +541 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-popup.tsx +54 -22
- package/src/components/silke-button/silke-button.scss +3 -3
- package/src/components/silke-form-schema/silke-schema-field.tsx +1 -1
- package/src/components/silke-popover-native/index.ts +1 -0
- package/src/components/silke-popover-native/silke-popover-native.mdx +207 -0
- package/src/components/silke-popover-native/silke-popover-native.scss +112 -0
- package/src/components/silke-popover-native/silke-popover-native.tsx +172 -0
- package/src/components/silke-popover-native/types.ts +21 -0
- package/src/components/silke-popover-native/use-popover-positioning.ts +159 -0
- package/src/components/silke-select-field/index.ts +1 -0
- package/src/components/silke-select-field/silke-select-field-native.tsx +17 -0
- package/src/components/silke-select-field/silke-select-field.mdx +65 -0
- package/src/components/silke-text-field/silke-text-field.tsx +2 -1
- package/src/icons.tsx +2 -0
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Popover Native
|
|
2
|
+
|
|
3
|
+
Native popover implementation using the browser's [Popover API](https://developer.mozilla.org/en-US/docs/Web/API/Popover_API/)
|
|
4
|
+
|
|
5
|
+
```js
|
|
6
|
+
const [show, setShow] = React.useState(false);
|
|
7
|
+
<>
|
|
8
|
+
<SilkeButton label="Show popover" onClick={() => setShow(true)} />
|
|
9
|
+
<SilkePopoverNative hide={!show} onRequestClose={() => setShow(false)}>
|
|
10
|
+
<SilkeBox bg="neutral-20" pad column>
|
|
11
|
+
<h1>Simple popover example</h1>
|
|
12
|
+
<SilkeButton label="Close" onClick={() => setShow(false)} />
|
|
13
|
+
</SilkeBox>
|
|
14
|
+
</SilkePopoverNative>
|
|
15
|
+
</>;
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Placement Options
|
|
19
|
+
|
|
20
|
+
The "placement" prop controls where the popover appears relative to the anchor. Use "-start" or "-end" to align the popover's edges with the anchor's edges:
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
const [show, setShow] = React.useState(false);
|
|
24
|
+
const [placement, setPlacement] = React.useState('bottom');
|
|
25
|
+
const placements = [
|
|
26
|
+
'top',
|
|
27
|
+
'top-start',
|
|
28
|
+
'top-end',
|
|
29
|
+
'right',
|
|
30
|
+
'right-start',
|
|
31
|
+
'right-end',
|
|
32
|
+
'bottom',
|
|
33
|
+
'bottom-start',
|
|
34
|
+
'bottom-end',
|
|
35
|
+
'left',
|
|
36
|
+
'left-start',
|
|
37
|
+
'left-end',
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
const PlacementSelect = () => {
|
|
41
|
+
const groups = placements.reduce((acc, p) => {
|
|
42
|
+
const group = p.split('-')[0];
|
|
43
|
+
if (!acc[group]) {
|
|
44
|
+
acc[group] = [];
|
|
45
|
+
}
|
|
46
|
+
acc[group].push(p);
|
|
47
|
+
return acc;
|
|
48
|
+
}, {});
|
|
49
|
+
return (
|
|
50
|
+
<SilkeBox column gap="s">
|
|
51
|
+
{Object.entries(groups).map(([group, placements]) => {
|
|
52
|
+
return (
|
|
53
|
+
<SilkeBox key={group} column gap="s">
|
|
54
|
+
<p>{group}</p>
|
|
55
|
+
<SilkeBox flex wrap gap="s">
|
|
56
|
+
{placements.map((p) => (
|
|
57
|
+
<SilkeButton key={p} label={p} onClick={() => setPlacement(p)} />
|
|
58
|
+
))}
|
|
59
|
+
</SilkeBox>
|
|
60
|
+
</SilkeBox>
|
|
61
|
+
);
|
|
62
|
+
})}
|
|
63
|
+
</SilkeBox>
|
|
64
|
+
);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const ref = React.useRef();
|
|
68
|
+
<>
|
|
69
|
+
<SilkeButton ref={ref} label="Show popover" onClick={() => setShow(true)} />
|
|
70
|
+
<SilkePopoverNative
|
|
71
|
+
hide={!show}
|
|
72
|
+
anchor={ref}
|
|
73
|
+
placement={placement}
|
|
74
|
+
hideBackdrop
|
|
75
|
+
onRequestClose={() => setShow(false)}
|
|
76
|
+
>
|
|
77
|
+
<SilkeBox bg="neutral-20" pad column gap="s">
|
|
78
|
+
<h3>Placement</h3>
|
|
79
|
+
<PlacementSelect />
|
|
80
|
+
<SilkeButton label="Close" onClick={() => setShow(false)} />
|
|
81
|
+
</SilkeBox>
|
|
82
|
+
</SilkePopoverNative>
|
|
83
|
+
</>;
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## With Offset
|
|
87
|
+
|
|
88
|
+
You can offset the popover position from the anchor point using "offsetX" and "offsetY":
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
const [show, setShow] = React.useState(false);
|
|
92
|
+
const ref = React.useRef();
|
|
93
|
+
<>
|
|
94
|
+
<SilkeButton ref={ref} label="Show popover" onClick={() => setShow(true)} />
|
|
95
|
+
<SilkePopoverNative
|
|
96
|
+
hide={!show}
|
|
97
|
+
anchor={ref}
|
|
98
|
+
placement="bottom"
|
|
99
|
+
offsetX={100}
|
|
100
|
+
offsetY={50}
|
|
101
|
+
onRequestClose={() => setShow(false)}
|
|
102
|
+
>
|
|
103
|
+
<SilkeBox bg="neutral-20" pad column>
|
|
104
|
+
<h2>Popover with offset</h2>
|
|
105
|
+
<p>Offset 100px right, 50px down</p>
|
|
106
|
+
<SilkeButton label="Close" onClick={() => setShow(false)} />
|
|
107
|
+
</SilkeBox>
|
|
108
|
+
</SilkePopoverNative>
|
|
109
|
+
</>;
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Hint Mode (Hover)
|
|
113
|
+
|
|
114
|
+
Use the "hint" prop to show the popover on hover using the [Interest Invoker API](https://css-tricks.com/a-first-look-at-the-interest-invoker-api-for-hover-triggered-popovers/). It automatically appears when hovering the anchor and stays visible when hovering the popover itself. No JavaScript is required - the browser handles all hover interactions:
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
const ref = React.useRef();
|
|
118
|
+
<>
|
|
119
|
+
<SilkeButton ref={ref} label="Hover me for a hint">
|
|
120
|
+
<SilkeIcon icon="info" />
|
|
121
|
+
</SilkeButton>
|
|
122
|
+
<SilkePopoverNative hint anchor={ref} placement="bottom" hideBackdrop>
|
|
123
|
+
<SilkeBox bg="neutral-20" pad="s" rounded="small">
|
|
124
|
+
<p>This is a helpful hint that appears on hover!</p>
|
|
125
|
+
</SilkeBox>
|
|
126
|
+
</SilkePopoverNative>
|
|
127
|
+
</>;
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Without Backdrop
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
const [show, setShow] = React.useState(false);
|
|
134
|
+
<>
|
|
135
|
+
<SilkeButton label="Show popover (no backdrop)" onClick={() => setShow(true)} />
|
|
136
|
+
<SilkePopoverNative hide={!show} hideBackdrop onRequestClose={() => setShow(false)}>
|
|
137
|
+
<SilkeBox bg="neutral-20" pad column>
|
|
138
|
+
<h2>Popover without backdrop</h2>
|
|
139
|
+
<SilkeButton label="Close" onClick={() => setShow(false)} />
|
|
140
|
+
</SilkeBox>
|
|
141
|
+
</SilkePopoverNative>
|
|
142
|
+
</>;
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Auto-dismiss on Click Outside
|
|
146
|
+
|
|
147
|
+
The native popover automatically dismisses when clicking outside (light dismiss) or pressing Escape. Use "onRequestClose" to handle the dismissal:
|
|
148
|
+
|
|
149
|
+
```js
|
|
150
|
+
const [show, setShow] = React.useState(false);
|
|
151
|
+
<>
|
|
152
|
+
<SilkeButton label="Show popover" onClick={() => setShow(true)} />
|
|
153
|
+
<SilkePopoverNative
|
|
154
|
+
hide={!show}
|
|
155
|
+
onRequestClose={() => {
|
|
156
|
+
console.log('Popover dismissed');
|
|
157
|
+
setShow(false);
|
|
158
|
+
}}
|
|
159
|
+
>
|
|
160
|
+
<SilkeBox bg="neutral-20" pad column>
|
|
161
|
+
<h2>Click outside or press Escape to dismiss</h2>
|
|
162
|
+
<SilkeButton label="Close manually" onClick={() => setShow(false)} />
|
|
163
|
+
</SilkeBox>
|
|
164
|
+
</SilkePopoverNative>
|
|
165
|
+
</>;
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Nested popovers
|
|
169
|
+
|
|
170
|
+
```js
|
|
171
|
+
const [show, setShow] = React.useState(false);
|
|
172
|
+
const [nestedShow, setNestedShow] = React.useState(false);
|
|
173
|
+
const buttonRef = React.useRef();
|
|
174
|
+
const nestedButtonRef = React.useRef();
|
|
175
|
+
|
|
176
|
+
<>
|
|
177
|
+
<SilkeButton ref={buttonRef} label="Show popover" onClick={() => setShow(true)} />
|
|
178
|
+
|
|
179
|
+
<SilkePopoverNative
|
|
180
|
+
hide={!show}
|
|
181
|
+
anchor={buttonRef}
|
|
182
|
+
placement="bottom"
|
|
183
|
+
hideBackdrop
|
|
184
|
+
onRequestClose={() => setShow(false)}
|
|
185
|
+
>
|
|
186
|
+
<SilkeBox bg="neutral-20" pad column>
|
|
187
|
+
<h2>Main popover</h2>
|
|
188
|
+
<SilkeButton
|
|
189
|
+
ref={nestedButtonRef}
|
|
190
|
+
label="Show nested popover"
|
|
191
|
+
onClick={() => setNestedShow(true)}
|
|
192
|
+
/>
|
|
193
|
+
</SilkeBox>
|
|
194
|
+
<SilkePopoverNative
|
|
195
|
+
hide={!nestedShow}
|
|
196
|
+
anchor={nestedButtonRef}
|
|
197
|
+
placement="right"
|
|
198
|
+
hideBackdrop
|
|
199
|
+
onRequestClose={() => setNestedShow(false)}
|
|
200
|
+
>
|
|
201
|
+
<SilkeBox bg="neutral-20" pad column>
|
|
202
|
+
<h2>Nested popover</h2>
|
|
203
|
+
</SilkeBox>
|
|
204
|
+
</SilkePopoverNative>
|
|
205
|
+
</SilkePopoverNative>
|
|
206
|
+
</>;
|
|
207
|
+
```
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
@use '../../variables' as *;
|
|
2
|
+
|
|
3
|
+
.popover {
|
|
4
|
+
position: fixed;
|
|
5
|
+
max-height: 100vh;
|
|
6
|
+
max-width: 100vw;
|
|
7
|
+
z-index: 999;
|
|
8
|
+
margin: 0;
|
|
9
|
+
border: none;
|
|
10
|
+
padding: 0;
|
|
11
|
+
background: transparent;
|
|
12
|
+
color: inherit;
|
|
13
|
+
|
|
14
|
+
/* Reset positioning properties - will be set by position-area when anchored */
|
|
15
|
+
top: auto;
|
|
16
|
+
left: auto;
|
|
17
|
+
transform: none;
|
|
18
|
+
|
|
19
|
+
/* Center popover when no anchor is provided */
|
|
20
|
+
&:not([style*="position-anchor"]) {
|
|
21
|
+
top: 50%;
|
|
22
|
+
left: 50%;
|
|
23
|
+
transform: translate(-50%, -50%);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/* Allow automatic position adjustment */
|
|
27
|
+
position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
|
|
28
|
+
|
|
29
|
+
/* Default: hide backdrop for regular popovers by making it transparent */
|
|
30
|
+
&::backdrop {
|
|
31
|
+
background: transparent;
|
|
32
|
+
pointer-events: none;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* Modal backdrop - enabled via showBackdrop prop */
|
|
37
|
+
.popover[data-show-backdrop="true"]::backdrop {
|
|
38
|
+
background-color: var(--modal-overlay-background);
|
|
39
|
+
pointer-events: auto;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/* Interest Invoker API - use the shorthand: */
|
|
43
|
+
[interestfor] {
|
|
44
|
+
interest-delay: 0ms 100ms;
|
|
45
|
+
--interest-delay: 0ms 100ms;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/* Animations */
|
|
49
|
+
|
|
50
|
+
.popover[data-animate="true"]:popover-open {
|
|
51
|
+
animation: fade-in 0.2s ease;
|
|
52
|
+
}
|
|
53
|
+
.popover[data-animate="true"]:popover-open::backdrop {
|
|
54
|
+
animation: fade-in-backdrop 0.1s ease;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
.popover[data-animate="true"][data-placement="top"] {
|
|
58
|
+
transform-origin: bottom center;
|
|
59
|
+
}
|
|
60
|
+
.popover[data-animate="true"][data-placement="bottom"] {
|
|
61
|
+
transform-origin: top center;
|
|
62
|
+
}
|
|
63
|
+
.popover[data-animate="true"][data-placement="left"] {
|
|
64
|
+
transform-origin: right center;
|
|
65
|
+
}
|
|
66
|
+
.popover[data-animate="true"][data-placement="right"] {
|
|
67
|
+
transform-origin: left center;
|
|
68
|
+
}
|
|
69
|
+
.popover[data-animate="true"][data-placement="top-start"] {
|
|
70
|
+
transform-origin: bottom left;
|
|
71
|
+
}
|
|
72
|
+
.popover[data-animate="true"][data-placement="top-end"] {
|
|
73
|
+
transform-origin: bottom right;
|
|
74
|
+
}
|
|
75
|
+
.popover[data-animate="true"][data-placement="bottom-start"] {
|
|
76
|
+
transform-origin: top left;
|
|
77
|
+
}
|
|
78
|
+
.popover[data-animate="true"][data-placement="bottom-end"] {
|
|
79
|
+
transform-origin: top right;
|
|
80
|
+
}
|
|
81
|
+
.popover[data-animate="true"][data-placement="left-start"] {
|
|
82
|
+
transform-origin: right top;
|
|
83
|
+
}
|
|
84
|
+
.popover[data-animate="true"][data-placement="left-end"] {
|
|
85
|
+
transform-origin: right bottom;
|
|
86
|
+
}
|
|
87
|
+
.popover[data-animate="true"][data-placement="right-start"] {
|
|
88
|
+
transform-origin: left top;
|
|
89
|
+
}
|
|
90
|
+
.popover[data-animate="true"][data-placement="right-end"] {
|
|
91
|
+
transform-origin: left bottom;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@keyframes fade-in {
|
|
96
|
+
from {
|
|
97
|
+
scale: 0.96;
|
|
98
|
+
opacity: 0;
|
|
99
|
+
}
|
|
100
|
+
to {
|
|
101
|
+
opacity: 1;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
@keyframes fade-in-backdrop {
|
|
106
|
+
from {
|
|
107
|
+
opacity: 0;
|
|
108
|
+
}
|
|
109
|
+
to {
|
|
110
|
+
opacity: 1;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// Polyfill for Interest Invoker API (for hover-triggered popovers)
|
|
2
|
+
import 'invokers';
|
|
3
|
+
|
|
4
|
+
import React, { useEffect, useLayoutEffect, useMemo, useRef } from 'react';
|
|
5
|
+
import { getPlacementStyles, usePopoverPositioning } from './use-popover-positioning';
|
|
6
|
+
import type { PopoverAnchorNative, PopoverPlacement, PopoverToggleEvent } from './types';
|
|
7
|
+
import styles from './silke-popover-native.scss';
|
|
8
|
+
|
|
9
|
+
function generateAnchorName(): string {
|
|
10
|
+
return `--anchor-${Math.random().toString(36).slice(2, 11)}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface SilkePopoverNativeProps {
|
|
14
|
+
id?: string;
|
|
15
|
+
/** Should the popover be hidden */
|
|
16
|
+
hide?: boolean;
|
|
17
|
+
/** Children to add inside the popover */
|
|
18
|
+
children?: React.ReactElement | React.ReactElement[];
|
|
19
|
+
/** Element to anchor the popover to (ref) */
|
|
20
|
+
anchor?: PopoverAnchorNative;
|
|
21
|
+
/** Where to place the popover relative to the anchor. 'start' aligns the start of the popover with the start of the anchor. 'end' aligns the end of the popover with the end of the anchor. */
|
|
22
|
+
placement?: PopoverPlacement;
|
|
23
|
+
/** Advanced: What slot of the anchor should be used for positioning (overrides placement) */
|
|
24
|
+
anchorSlot?: string;
|
|
25
|
+
/** Advanced: Vertical alignment of the popover (overrides placement) */
|
|
26
|
+
align?: string;
|
|
27
|
+
/** Advanced: Horizontal alignment of the popover (overrides placement) */
|
|
28
|
+
justify?: string;
|
|
29
|
+
/** Offset the popover position horizontally */
|
|
30
|
+
offsetX?: number;
|
|
31
|
+
/** Offset the popover position vertically */
|
|
32
|
+
offsetY?: number;
|
|
33
|
+
/** Should the popover have a backdrop */
|
|
34
|
+
hideBackdrop?: boolean;
|
|
35
|
+
/** Should the popover match the width of the anchor */
|
|
36
|
+
matchWidth?: boolean;
|
|
37
|
+
/** Show popover on hover (hint mode) - automatically shows on anchor hover and stays visible when hovering popover */
|
|
38
|
+
hint?: boolean;
|
|
39
|
+
/** Should the popover be animated */
|
|
40
|
+
animate?: boolean;
|
|
41
|
+
/** Triggered when the popover is dismissed (via light dismiss or escape key) */
|
|
42
|
+
onRequestClose?: () => void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function SilkePopoverNative({
|
|
46
|
+
anchor,
|
|
47
|
+
children,
|
|
48
|
+
hide = true,
|
|
49
|
+
id,
|
|
50
|
+
placement = 'bottom',
|
|
51
|
+
anchorSlot,
|
|
52
|
+
align,
|
|
53
|
+
justify,
|
|
54
|
+
offsetX = 0,
|
|
55
|
+
offsetY = 0,
|
|
56
|
+
hideBackdrop,
|
|
57
|
+
matchWidth = false,
|
|
58
|
+
hint = false,
|
|
59
|
+
animate = false,
|
|
60
|
+
onRequestClose,
|
|
61
|
+
}: SilkePopoverNativeProps) {
|
|
62
|
+
const popoverRef = useRef<HTMLDivElement>(null);
|
|
63
|
+
const popoverId = id || `silke-popover-native-${Math.random().toString(36).slice(2, 11)}`;
|
|
64
|
+
const anchorName = useMemo(() => generateAnchorName(), []);
|
|
65
|
+
|
|
66
|
+
// Convert placement to low-level props, or use provided overrides
|
|
67
|
+
const placementStyles = useMemo(() => {
|
|
68
|
+
let styles = getPlacementStyles(placement);
|
|
69
|
+
if (anchorSlot) {
|
|
70
|
+
styles = { ...styles, positionArea: anchorSlot };
|
|
71
|
+
}
|
|
72
|
+
if (align) {
|
|
73
|
+
styles = { ...styles, alignSelf: align };
|
|
74
|
+
}
|
|
75
|
+
if (justify) {
|
|
76
|
+
styles = { ...styles, justifySelf: justify };
|
|
77
|
+
}
|
|
78
|
+
return styles;
|
|
79
|
+
}, [placement, anchorSlot, align, justify]);
|
|
80
|
+
|
|
81
|
+
usePopoverPositioning(
|
|
82
|
+
popoverRef,
|
|
83
|
+
anchor,
|
|
84
|
+
anchorName,
|
|
85
|
+
placementStyles,
|
|
86
|
+
offsetX,
|
|
87
|
+
offsetY,
|
|
88
|
+
matchWidth,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
// Set popover attribute synchronously on mount (before hide/show effect runs)
|
|
92
|
+
useLayoutEffect(() => {
|
|
93
|
+
const popover = popoverRef.current;
|
|
94
|
+
const anchorElement = anchor?.current;
|
|
95
|
+
|
|
96
|
+
if (popover) {
|
|
97
|
+
const popoverMode = hint ? 'hint' : 'auto';
|
|
98
|
+
if (popover.getAttribute('popover') !== popoverMode) {
|
|
99
|
+
popover.setAttribute('popover', popoverMode);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Set interestfor on anchor for hint mode
|
|
104
|
+
if (hint && anchorElement) {
|
|
105
|
+
anchorElement.setAttribute('interestfor', popoverId);
|
|
106
|
+
} else if (anchorElement) {
|
|
107
|
+
anchorElement.removeAttribute('interestfor');
|
|
108
|
+
}
|
|
109
|
+
}, [hint, anchor, popoverId]);
|
|
110
|
+
|
|
111
|
+
// Handle show/hide state
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
if (hint) return;
|
|
114
|
+
|
|
115
|
+
const popover = popoverRef.current;
|
|
116
|
+
if (!popover || !popover.hasAttribute('popover')) return;
|
|
117
|
+
|
|
118
|
+
const shouldShow = !hide;
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
if (shouldShow) {
|
|
122
|
+
if (!popover.matches(':popover-open')) {
|
|
123
|
+
popover.showPopover();
|
|
124
|
+
}
|
|
125
|
+
} else {
|
|
126
|
+
if (popover.matches(':popover-open')) {
|
|
127
|
+
popover.hidePopover();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
} catch (e) {
|
|
131
|
+
console.warn('Popover API error:', e);
|
|
132
|
+
}
|
|
133
|
+
}, [hide, hint]);
|
|
134
|
+
|
|
135
|
+
// Handle dismiss events (light dismiss and escape key)
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
const popover = popoverRef.current;
|
|
138
|
+
if (!popover || !onRequestClose) return;
|
|
139
|
+
|
|
140
|
+
const handleToggle = (e: Event) => {
|
|
141
|
+
const toggleEvent = e as PopoverToggleEvent;
|
|
142
|
+
if (toggleEvent.newState === 'closed' && onRequestClose) {
|
|
143
|
+
onRequestClose();
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
popover.addEventListener('toggle', handleToggle);
|
|
148
|
+
|
|
149
|
+
return () => {
|
|
150
|
+
popover.removeEventListener('toggle', handleToggle);
|
|
151
|
+
};
|
|
152
|
+
}, [onRequestClose]);
|
|
153
|
+
|
|
154
|
+
// Determine role based on backdrop (modal vs dialog vs region)
|
|
155
|
+
const role = !hideBackdrop ? 'dialog' : 'region';
|
|
156
|
+
const ariaModal = !hideBackdrop ? { 'aria-modal': 'true' as const } : {};
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<div
|
|
160
|
+
ref={popoverRef}
|
|
161
|
+
id={popoverId}
|
|
162
|
+
className={styles.popover}
|
|
163
|
+
data-placement={placement}
|
|
164
|
+
data-show-backdrop={hideBackdrop ? 'false' : 'true'}
|
|
165
|
+
data-animate={animate ? 'true' : 'false'}
|
|
166
|
+
role={role}
|
|
167
|
+
{...ariaModal}
|
|
168
|
+
>
|
|
169
|
+
{children}
|
|
170
|
+
</div>
|
|
171
|
+
);
|
|
172
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type PopoverPlacement =
|
|
2
|
+
| 'top'
|
|
3
|
+
| 'top-start'
|
|
4
|
+
| 'top-end'
|
|
5
|
+
| 'right'
|
|
6
|
+
| 'right-start'
|
|
7
|
+
| 'right-end'
|
|
8
|
+
| 'bottom'
|
|
9
|
+
| 'bottom-start'
|
|
10
|
+
| 'bottom-end'
|
|
11
|
+
| 'left'
|
|
12
|
+
| 'left-start'
|
|
13
|
+
| 'left-end';
|
|
14
|
+
|
|
15
|
+
export type PopoverAnchorNative = React.RefObject<HTMLElement>;
|
|
16
|
+
|
|
17
|
+
// ToggleEvent is part of the Popover API but not yet in TypeScript's DOM types
|
|
18
|
+
export interface PopoverToggleEvent extends Event {
|
|
19
|
+
newState: 'open' | 'closed';
|
|
20
|
+
oldState: 'open' | 'closed';
|
|
21
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { useEffect, type CSSProperties } from 'react';
|
|
2
|
+
import type { PopoverAnchorNative, PopoverPlacement } from './types';
|
|
3
|
+
|
|
4
|
+
export function usePopoverPositioning(
|
|
5
|
+
popoverRef: React.RefObject<HTMLDivElement>,
|
|
6
|
+
anchor: PopoverAnchorNative | undefined,
|
|
7
|
+
anchorName: string,
|
|
8
|
+
placementStyles: ExtendedCSSProperties,
|
|
9
|
+
offsetX = 0,
|
|
10
|
+
offsetY = 0,
|
|
11
|
+
matchWidth = false,
|
|
12
|
+
) {
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
const popover = popoverRef.current;
|
|
15
|
+
const anchorElement = anchor?.current;
|
|
16
|
+
|
|
17
|
+
if (!popover) return;
|
|
18
|
+
|
|
19
|
+
// Reset all popover styles
|
|
20
|
+
Object.keys(popover.style).forEach((key: string) => {
|
|
21
|
+
popover.style.removeProperty(key);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (!anchorElement) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Reset all popover styles
|
|
29
|
+
Object.keys(popover.style).forEach((key: string) => {
|
|
30
|
+
popover.style.removeProperty(key);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
if (anchorName) {
|
|
34
|
+
anchorElement.style.setProperty('anchor-name', anchorName);
|
|
35
|
+
popover.style.setProperty('position-anchor', anchorName);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Map camelCase properties to kebab-case CSS properties
|
|
39
|
+
const propertyMap: Record<string, string> = {
|
|
40
|
+
positionArea: 'position-area',
|
|
41
|
+
alignSelf: 'align-self',
|
|
42
|
+
justifySelf: 'justify-self',
|
|
43
|
+
positionTryFallbacks: 'position-try-fallbacks',
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// Apply all placement styles
|
|
47
|
+
Object.entries(placementStyles).forEach(([key, value]) => {
|
|
48
|
+
if (value !== undefined && value !== null) {
|
|
49
|
+
const cssProperty = propertyMap[key] || key;
|
|
50
|
+
const cssValue = typeof value === 'string' ? value : String(value);
|
|
51
|
+
popover.style.setProperty(cssProperty, cssValue);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
if (matchWidth) {
|
|
56
|
+
// Get width of anchor
|
|
57
|
+
const anchorWidth = anchorElement.getBoundingClientRect().width;
|
|
58
|
+
popover.style.width = anchorWidth + 'px';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (offsetX !== 0 || offsetY !== 0) {
|
|
62
|
+
popover.style.marginLeft = offsetX + 'px';
|
|
63
|
+
popover.style.marginTop = offsetY + 'px';
|
|
64
|
+
}
|
|
65
|
+
}, [popoverRef, anchor, anchorName, placementStyles, offsetX, offsetY, matchWidth]);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type ExtendedCSSProperties = CSSProperties & {
|
|
69
|
+
positionArea?: string;
|
|
70
|
+
positionTryFallbacks?: string;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// Only needed to simplify props, user can also use advanced placement styles for more control
|
|
74
|
+
export function getPlacementStyles(placement: PopoverPlacement): ExtendedCSSProperties {
|
|
75
|
+
const baseStyles: ExtendedCSSProperties = { margin: 0 };
|
|
76
|
+
|
|
77
|
+
switch (placement) {
|
|
78
|
+
case 'bottom':
|
|
79
|
+
return {
|
|
80
|
+
...baseStyles,
|
|
81
|
+
positionArea: 'bottom',
|
|
82
|
+
justifySelf: 'anchor-center',
|
|
83
|
+
};
|
|
84
|
+
case 'bottom-start':
|
|
85
|
+
return {
|
|
86
|
+
...baseStyles,
|
|
87
|
+
positionArea: 'bottom span-right',
|
|
88
|
+
justifySelf: 'start',
|
|
89
|
+
};
|
|
90
|
+
case 'bottom-end':
|
|
91
|
+
return {
|
|
92
|
+
...baseStyles,
|
|
93
|
+
positionArea: 'bottom span-left',
|
|
94
|
+
justifySelf: 'end',
|
|
95
|
+
};
|
|
96
|
+
case 'top':
|
|
97
|
+
return {
|
|
98
|
+
...baseStyles,
|
|
99
|
+
positionArea: 'top',
|
|
100
|
+
justifySelf: 'anchor-center',
|
|
101
|
+
};
|
|
102
|
+
case 'top-start':
|
|
103
|
+
return {
|
|
104
|
+
...baseStyles,
|
|
105
|
+
positionArea: 'top span-right',
|
|
106
|
+
justifySelf: 'start',
|
|
107
|
+
};
|
|
108
|
+
case 'top-end':
|
|
109
|
+
return {
|
|
110
|
+
...baseStyles,
|
|
111
|
+
positionArea: 'top span-left',
|
|
112
|
+
justifySelf: 'end',
|
|
113
|
+
};
|
|
114
|
+
case 'right':
|
|
115
|
+
return {
|
|
116
|
+
...baseStyles,
|
|
117
|
+
positionArea: 'none',
|
|
118
|
+
left: 'anchor(right)',
|
|
119
|
+
top: 'anchor(center)',
|
|
120
|
+
transform: 'translateY(-50%)',
|
|
121
|
+
positionTryFallbacks: 'none',
|
|
122
|
+
};
|
|
123
|
+
case 'right-start':
|
|
124
|
+
return {
|
|
125
|
+
...baseStyles,
|
|
126
|
+
positionArea: 'span-bottom right',
|
|
127
|
+
alignSelf: 'start',
|
|
128
|
+
};
|
|
129
|
+
case 'right-end':
|
|
130
|
+
return {
|
|
131
|
+
...baseStyles,
|
|
132
|
+
positionArea: 'span-top right',
|
|
133
|
+
alignSelf: 'end',
|
|
134
|
+
};
|
|
135
|
+
case 'left':
|
|
136
|
+
return {
|
|
137
|
+
...baseStyles,
|
|
138
|
+
positionArea: 'none',
|
|
139
|
+
right: 'anchor(left)',
|
|
140
|
+
top: 'anchor(center)',
|
|
141
|
+
transform: 'translateY(-50%)',
|
|
142
|
+
positionTryFallbacks: 'none',
|
|
143
|
+
};
|
|
144
|
+
case 'left-start':
|
|
145
|
+
return {
|
|
146
|
+
...baseStyles,
|
|
147
|
+
positionArea: 'span-bottom left',
|
|
148
|
+
alignSelf: 'start',
|
|
149
|
+
};
|
|
150
|
+
case 'left-end':
|
|
151
|
+
return {
|
|
152
|
+
...baseStyles,
|
|
153
|
+
positionArea: 'span-top left',
|
|
154
|
+
alignSelf: 'end',
|
|
155
|
+
};
|
|
156
|
+
default:
|
|
157
|
+
return baseStyles;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
SilkeAutocompleteFieldNative,
|
|
4
|
+
SilkeAutocompleteFieldNativeProps,
|
|
5
|
+
} from '../silke-autocomplete-field';
|
|
6
|
+
import { Entry } from '../silke-autocomplete-field/autocomplete-types';
|
|
7
|
+
|
|
8
|
+
export type SilkeSelectFieldNativeProps<T, U extends Entry<T> = any> = Omit<
|
|
9
|
+
SilkeAutocompleteFieldNativeProps<T, U>,
|
|
10
|
+
'disableSearch' | 'noReset'
|
|
11
|
+
>;
|
|
12
|
+
|
|
13
|
+
export function SilkeSelectFieldNative<T, U extends Entry<T>>(
|
|
14
|
+
props: SilkeSelectFieldNativeProps<T, U>,
|
|
15
|
+
) {
|
|
16
|
+
return <SilkeAutocompleteFieldNative {...(props as any)} disableSearch noReset />;
|
|
17
|
+
}
|