@human-kit/ui 1.0.0-beta.6 → 1.0.0-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/FOCUS_STATE_CONTRACT.md +15 -4
- package/dist/FOCUS_STATE_REVIEW_TEMPLATE.md +8 -8
- package/dist/autocomplete/item/autocomplete-item.svelte +10 -0
- package/dist/avatar/README.md +52 -0
- package/dist/avatar/count/avatar-count.svelte +56 -0
- package/dist/avatar/count/avatar-count.svelte.d.ts +4 -0
- package/dist/avatar/fallback/avatar-fallback.svelte +67 -0
- package/dist/avatar/fallback/avatar-fallback.svelte.d.ts +4 -0
- package/dist/avatar/group/avatar-group.svelte +65 -0
- package/dist/avatar/group/avatar-group.svelte.d.ts +4 -0
- package/dist/avatar/group/context.d.ts +15 -0
- package/dist/avatar/group/context.js +15 -0
- package/dist/avatar/image/avatar-image.svelte +104 -0
- package/dist/avatar/image/avatar-image.svelte.d.ts +4 -0
- package/dist/avatar/index.d.ts +22 -0
- package/dist/avatar/index.js +10 -0
- package/dist/avatar/index.parts.d.ts +5 -0
- package/dist/avatar/index.parts.js +5 -0
- package/dist/avatar/root/avatar-root.svelte +79 -0
- package/dist/avatar/root/avatar-root.svelte.d.ts +4 -0
- package/dist/avatar/root/context.d.ts +14 -0
- package/dist/avatar/root/context.js +15 -0
- package/dist/avatar/types.d.ts +84 -0
- package/dist/avatar/types.js +1 -0
- package/dist/breadcrumbs/README.md +65 -0
- package/dist/breadcrumbs/ellipsis/breadcrumbs-ellipsis.svelte +62 -0
- package/dist/breadcrumbs/ellipsis/breadcrumbs-ellipsis.svelte.d.ts +4 -0
- package/dist/breadcrumbs/index.d.ts +23 -0
- package/dist/breadcrumbs/index.js +10 -0
- package/dist/breadcrumbs/index.parts.d.ts +6 -0
- package/dist/breadcrumbs/index.parts.js +6 -0
- package/dist/breadcrumbs/item/breadcrumbs-item.svelte +42 -0
- package/dist/breadcrumbs/item/breadcrumbs-item.svelte.d.ts +4 -0
- package/dist/breadcrumbs/link/breadcrumbs-link.svelte +47 -0
- package/dist/breadcrumbs/link/breadcrumbs-link.svelte.d.ts +4 -0
- package/dist/breadcrumbs/list/breadcrumbs-list.svelte +31 -0
- package/dist/breadcrumbs/list/breadcrumbs-list.svelte.d.ts +4 -0
- package/dist/breadcrumbs/root/breadcrumbs-root.svelte +112 -0
- package/dist/breadcrumbs/root/breadcrumbs-root.svelte.d.ts +4 -0
- package/dist/breadcrumbs/root/context.d.ts +19 -0
- package/dist/breadcrumbs/root/context.js +15 -0
- package/dist/breadcrumbs/separator/breadcrumbs-separator.svelte +35 -0
- package/dist/breadcrumbs/separator/breadcrumbs-separator.svelte.d.ts +4 -0
- package/dist/breadcrumbs/types.d.ts +80 -0
- package/dist/breadcrumbs/types.js +1 -0
- package/dist/button/root/button-root.svelte +9 -0
- package/dist/calendar/body-cell/calendar-body-cell.svelte +7 -0
- package/dist/checkbox/indicator/checkbox-indicator.svelte +1 -0
- package/dist/checkbox/indicator/checkbox-indicator.svelte.d.ts +1 -0
- package/dist/checkbox/root/checkbox-root.svelte +106 -30
- package/dist/checkbox/root/checkbox-root.svelte.d.ts +5 -0
- package/dist/checkbox-group/README.md +95 -0
- package/dist/checkbox-group/index.d.ts +15 -0
- package/dist/checkbox-group/index.js +6 -0
- package/dist/checkbox-group/index.parts.d.ts +4 -0
- package/dist/checkbox-group/index.parts.js +7 -0
- package/dist/checkbox-group/label/checkbox-group-label.svelte +36 -0
- package/dist/checkbox-group/label/checkbox-group-label.svelte.d.ts +4 -0
- package/dist/checkbox-group/root/checkbox-group-root.svelte +118 -0
- package/dist/checkbox-group/root/checkbox-group-root.svelte.d.ts +4 -0
- package/dist/checkbox-group/root/context.svelte.d.ts +52 -0
- package/dist/checkbox-group/root/context.svelte.js +251 -0
- package/dist/checkbox-group/types.d.ts +57 -0
- package/dist/checkbox-group/types.js +1 -0
- package/dist/combobox/popover/combobox-popover.svelte +4 -56
- package/dist/dialog/root/dialog-root.svelte +15 -2
- package/dist/drawer/root/drawer-root.svelte +15 -2
- package/dist/index.d.ts +18 -0
- package/dist/index.js +17 -0
- package/dist/internal/localized-strings.d.ts +48 -0
- package/dist/internal/localized-strings.js +48 -0
- package/dist/internal/number-precision.d.ts +9 -0
- package/dist/internal/number-precision.js +27 -0
- package/dist/menu/root/focus-state.d.ts +2 -2
- package/dist/menu/root/focus-state.js +8 -32
- package/dist/menu/root/menu-state.svelte.js +5 -1
- package/dist/numberfield/root/number-utils.js +1 -20
- package/dist/popover/root/focus-state.d.ts +2 -2
- package/dist/popover/root/focus-state.js +7 -34
- package/dist/primitives/contain-wheel.d.ts +9 -0
- package/dist/primitives/contain-wheel.js +41 -0
- package/dist/primitives/floating.d.ts +6 -0
- package/dist/primitives/floating.js +26 -5
- package/dist/primitives/focus-visible.svelte.d.ts +14 -0
- package/dist/primitives/focus-visible.svelte.js +19 -0
- package/dist/primitives/index.d.ts +1 -0
- package/dist/primitives/index.js +1 -0
- package/dist/primitives/input-modality.d.ts +7 -0
- package/dist/primitives/input-modality.js +32 -5
- package/dist/primitives/keyboard-navigation.d.ts +6 -0
- package/dist/primitives/keyboard-navigation.js +2 -1
- package/dist/primitives/trigger-focus-state.d.ts +29 -0
- package/dist/primitives/trigger-focus-state.js +57 -0
- package/dist/progress/README.md +54 -0
- package/dist/progress/index.d.ts +21 -0
- package/dist/progress/index.js +9 -0
- package/dist/progress/index.parts.d.ts +5 -0
- package/dist/progress/index.parts.js +5 -0
- package/dist/progress/indicator/progress-indicator.svelte +37 -0
- package/dist/progress/indicator/progress-indicator.svelte.d.ts +4 -0
- package/dist/progress/label/progress-label.svelte +34 -0
- package/dist/progress/label/progress-label.svelte.d.ts +4 -0
- package/dist/progress/root/context.d.ts +26 -0
- package/dist/progress/root/context.js +15 -0
- package/dist/progress/root/progress-root.svelte +136 -0
- package/dist/progress/root/progress-root.svelte.d.ts +4 -0
- package/dist/progress/root/progress-utils.d.ts +12 -0
- package/dist/progress/root/progress-utils.js +41 -0
- package/dist/progress/track/progress-track.svelte +27 -0
- package/dist/progress/track/progress-track.svelte.d.ts +4 -0
- package/dist/progress/types.d.ts +81 -0
- package/dist/progress/types.js +1 -0
- package/dist/progress/value/progress-value.svelte +36 -0
- package/dist/progress/value/progress-value.svelte.d.ts +4 -0
- package/dist/radio-group/README.md +103 -0
- package/dist/radio-group/index.d.ts +18 -0
- package/dist/radio-group/index.js +9 -0
- package/dist/radio-group/index.parts.d.ts +4 -0
- package/dist/radio-group/index.parts.js +4 -0
- package/dist/radio-group/indicator/radio-group-indicator.svelte +43 -0
- package/dist/radio-group/indicator/radio-group-indicator.svelte.d.ts +12 -0
- package/dist/radio-group/item/context.d.ts +18 -0
- package/dist/radio-group/item/context.js +15 -0
- package/dist/radio-group/item/radio-group-item.svelte +377 -0
- package/dist/radio-group/item/radio-group-item.svelte.d.ts +35 -0
- package/dist/radio-group/label/radio-group-label.svelte +37 -0
- package/dist/radio-group/label/radio-group-label.svelte.d.ts +4 -0
- package/dist/radio-group/root/context.svelte.d.ts +57 -0
- package/dist/radio-group/root/context.svelte.js +284 -0
- package/dist/radio-group/root/radio-group-root.svelte +193 -0
- package/dist/radio-group/root/radio-group-root.svelte.d.ts +4 -0
- package/dist/radio-group/types.d.ts +50 -0
- package/dist/radio-group/types.js +1 -0
- package/dist/select/README.md +129 -0
- package/dist/select/index.d.ts +30 -0
- package/dist/select/index.js +16 -0
- package/dist/select/index.parts.d.ts +8 -0
- package/dist/select/index.parts.js +9 -0
- package/dist/select/item/select-item.svelte +68 -0
- package/dist/select/item/select-item.svelte.d.ts +10 -0
- package/dist/select/item-indicator/select-item-indicator.svelte +55 -0
- package/dist/select/item-indicator/select-item-indicator.svelte.d.ts +4 -0
- package/dist/select/label/select-label.svelte +46 -0
- package/dist/select/label/select-label.svelte.d.ts +4 -0
- package/dist/select/list/select-list.svelte +241 -0
- package/dist/select/list/select-list.svelte.d.ts +62 -0
- package/dist/select/popover/select-popover.svelte +80 -0
- package/dist/select/popover/select-popover.svelte.d.ts +9 -0
- package/dist/select/root/context.d.ts +106 -0
- package/dist/select/root/context.js +22 -0
- package/dist/select/root/select-root.svelte +544 -0
- package/dist/select/root/select-root.svelte.d.ts +25 -0
- package/dist/select/trigger/select-trigger.svelte +192 -0
- package/dist/select/trigger/select-trigger.svelte.d.ts +4 -0
- package/dist/select/types.d.ts +133 -0
- package/dist/select/types.js +1 -0
- package/dist/select/value/select-value.svelte +61 -0
- package/dist/select/value/select-value.svelte.d.ts +4 -0
- package/dist/separator/README.md +35 -0
- package/dist/separator/index.d.ts +7 -0
- package/dist/separator/index.js +3 -0
- package/dist/separator/separator.svelte +254 -0
- package/dist/separator/separator.svelte.d.ts +45 -0
- package/dist/separator/types.d.ts +1 -0
- package/dist/separator/types.js +1 -0
- package/dist/slider/README.md +61 -0
- package/dist/slider/fill/slider-fill.svelte +40 -0
- package/dist/slider/fill/slider-fill.svelte.d.ts +4 -0
- package/dist/slider/index.d.ts +9 -0
- package/dist/slider/index.js +8 -0
- package/dist/slider/index.parts.d.ts +6 -0
- package/dist/slider/index.parts.js +6 -0
- package/dist/slider/label/slider-label.svelte +44 -0
- package/dist/slider/label/slider-label.svelte.d.ts +4 -0
- package/dist/slider/output/slider-output.svelte +46 -0
- package/dist/slider/output/slider-output.svelte.d.ts +4 -0
- package/dist/slider/root/context.d.ts +106 -0
- package/dist/slider/root/context.js +15 -0
- package/dist/slider/root/slider-root.svelte +545 -0
- package/dist/slider/root/slider-root.svelte.d.ts +4 -0
- package/dist/slider/root/slider-utils.d.ts +57 -0
- package/dist/slider/root/slider-utils.js +88 -0
- package/dist/slider/thumb/slider-thumb.svelte +276 -0
- package/dist/slider/thumb/slider-thumb.svelte.d.ts +4 -0
- package/dist/slider/track/slider-track.svelte +60 -0
- package/dist/slider/track/slider-track.svelte.d.ts +4 -0
- package/dist/slider/types.d.ts +144 -0
- package/dist/slider/types.js +1 -0
- package/dist/switch/root/switch-root.svelte +9 -0
- package/dist/table/column-header-cell/table-column-header-cell.svelte +24 -0
- package/dist/table/root/table-root.svelte +29 -6
- package/dist/timepicker/input/time-picker-input.svelte +12 -4
- package/dist/timepicker/input/time-picker-input.svelte.d.ts +12 -4
- package/dist/toggle/root/toggle-root.svelte +28 -0
- package/dist/toggle/root/toggle-root.svelte.d.ts +19 -0
- package/dist/toggle-group/README.md +6 -4
- package/dist/toggle-group/index.d.ts +2 -0
- package/dist/toggle-group/index.parts.d.ts +1 -0
- package/dist/toggle-group/index.parts.js +4 -0
- package/dist/tooltip/README.md +53 -0
- package/dist/tooltip/arrow/tooltip-arrow.svelte +36 -0
- package/dist/tooltip/arrow/tooltip-arrow.svelte.d.ts +4 -0
- package/dist/tooltip/content/tooltip-content.svelte +148 -0
- package/dist/tooltip/content/tooltip-content.svelte.d.ts +4 -0
- package/dist/tooltip/index.d.ts +8 -0
- package/dist/tooltip/index.js +7 -0
- package/dist/tooltip/index.parts.d.ts +5 -0
- package/dist/tooltip/index.parts.js +5 -0
- package/dist/tooltip/provider/tooltip-provider.svelte +36 -0
- package/dist/tooltip/provider/tooltip-provider.svelte.d.ts +4 -0
- package/dist/tooltip/root/context.d.ts +46 -0
- package/dist/tooltip/root/context.js +22 -0
- package/dist/tooltip/root/safe-polygon.d.ts +26 -0
- package/dist/tooltip/root/safe-polygon.js +66 -0
- package/dist/tooltip/root/tooltip-group.d.ts +26 -0
- package/dist/tooltip/root/tooltip-group.js +41 -0
- package/dist/tooltip/root/tooltip-root.svelte +454 -0
- package/dist/tooltip/root/tooltip-root.svelte.d.ts +4 -0
- package/dist/tooltip/trigger/tooltip-trigger.svelte +52 -0
- package/dist/tooltip/trigger/tooltip-trigger.svelte.d.ts +4 -0
- package/dist/tooltip/types.d.ts +105 -0
- package/dist/tooltip/types.js +1 -0
- package/package.json +46 -1
|
@@ -35,10 +35,19 @@ Canonical implementation lives in `primitives/input-modality.ts`:
|
|
|
35
35
|
|
|
36
36
|
## Restore focus
|
|
37
37
|
|
|
38
|
-
On overlay/popover close,
|
|
38
|
+
On overlay/popover close, the trigger shows only the focus it holds:
|
|
39
39
|
|
|
40
|
-
- `escape-key`
|
|
41
|
-
|
|
40
|
+
- `escape-key`, `close-press`, `imperative-action` => the focus returns to the trigger:
|
|
41
|
+
`data-focused=true`, and `data-focus-visible=true` when the close came from the keyboard.
|
|
42
|
+
- `outside-press` on a focusable element => the focus stays there, and the trigger shows no
|
|
43
|
+
focus attributes.
|
|
44
|
+
- `outside-press` on nothing focusable (the focus fell to the body) => the focus returns to
|
|
45
|
+
the trigger with the pointer modality: `data-focused=true` and `data-focus-visible` absent.
|
|
46
|
+
Without it a keyboard user has nowhere to continue from.
|
|
47
|
+
Inside a modal popover every outside press is of this kind, because the page is inert.
|
|
48
|
+
- `focus-out`, `scroll` => the focus stays where the user put it.
|
|
49
|
+
|
|
50
|
+
`focusFellToBody()` and `applyTriggerCloseFocusState()` in `primitives/trigger-focus-state.ts` are the shared implementation; Popover and Menu only decide which reasons return the focus.
|
|
42
51
|
|
|
43
52
|
## Recommended Implementation
|
|
44
53
|
|
|
@@ -55,7 +64,7 @@ On overlay/popover close, transient trigger state is allowed:
|
|
|
55
64
|
The following components implement this contract:
|
|
56
65
|
|
|
57
66
|
- **Popover** — trigger + content, restore focus on close.
|
|
58
|
-
- **Dialog** — trigger + overlay/content, nested stack support.
|
|
67
|
+
- **Dialog** — trigger + overlay/content, nested stack support. After an outside press the restore waits a frame, because the press moves the focus to the body after the close.
|
|
59
68
|
- **Drawer** — trigger + overlay/content, shares the modal layer stack with Dialog. Restore also covers the `swipe` close reason (resolves to `pointer`), and retries the trigger focus after `ariaHideOutside` lifts `inert`.
|
|
60
69
|
- **Menu** — trigger + content, restore focus on close (`menu/root/focus-state.ts`). `Menu.ContextTrigger` is the same contract on a surface instead of a button: it registers as the trigger ref, so Escape and item selection restore focus to it. It is a tab stop by default so there is something to restore focus to.
|
|
61
70
|
- **DatePicker** — segment spinbuttons, trigger, popover (calendar).
|
|
@@ -65,3 +74,5 @@ The following components implement this contract:
|
|
|
65
74
|
- **ListBox** — items with roving tabindex.
|
|
66
75
|
- **TransferList** — two ListBoxes plus move buttons. Focus is placed explicitly after every move, because the rows the user was on stop existing: it stays on the button while that button still has work, and otherwise follows the items to the destination list; a double-clicked row hands focus to whichever row took its place.
|
|
67
76
|
- **NumberField** — spinbutton input, stepper buttons, wheel, and pointer scrub area.
|
|
77
|
+
- **Slider** — one native range input per thumb. The thumb shows `data-focused` and `data-focus-visible` for its input, and the root shows `data-focus-within` and `data-focus-visible`. A press on the track focuses the thumb with the pointer modality, and a key press after it turns the ring on through `watchFocusVisible`.
|
|
78
|
+
- **Tooltip** — never takes the focus, and never moves it. It opens on a focus of the trigger only when `shouldShowFocusVisible` says the focus came from the keyboard, thus a focus that a script gave with the pointer modality opens nothing.
|
|
@@ -20,14 +20,14 @@ Fill this out for PRs that modify interaction, keyboard navigation, overlays, or
|
|
|
20
20
|
|
|
21
21
|
> Mark **OK/NA/FAIL** and add a short note when failing.
|
|
22
22
|
|
|
23
|
-
| Scenario | What to validate
|
|
24
|
-
| ---------------------------------- |
|
|
25
|
-
| Keyboard (Tab/Arrow/Home/End/Page) | `data-*` and ARIA stay in sync with logical focus
|
|
26
|
-
| Pointer (click/mousedown) | do not elevate `data-focus-visible` by default
|
|
27
|
-
| External blur | transient state cleanup (`data-focus-within`, `data-focus-visible`)
|
|
28
|
-
| Close restore (`escape-key`) | trigger: `data-focused=true`, `data-focus-visible=true` (when applicable)
|
|
29
|
-
| Close restore (`outside-press`) | trigger: `data-focused=true`, `data-focus-visible` absent
|
|
30
|
-
| Programmatic focus | does not break invariants or leave stale state
|
|
23
|
+
| Scenario | What to validate | Status | Note |
|
|
24
|
+
| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ | ---- |
|
|
25
|
+
| Keyboard (Tab/Arrow/Home/End/Page) | `data-*` and ARIA stay in sync with logical focus | | |
|
|
26
|
+
| Pointer (click/mousedown) | do not elevate `data-focus-visible` by default | | |
|
|
27
|
+
| External blur | transient state cleanup (`data-focus-within`, `data-focus-visible`) | | |
|
|
28
|
+
| Close restore (`escape-key`) | trigger: `data-focused=true`, `data-focus-visible=true` (when applicable) | | |
|
|
29
|
+
| Close restore (`outside-press`) | focusable target: focus stays there, trigger without focus attrs; nothing focusable: focus returns to the trigger, `data-focused=true`, `data-focus-visible` absent | | |
|
|
30
|
+
| Programmatic focus | does not break invariants or leave stale state | | |
|
|
31
31
|
|
|
32
32
|
## 3) Required Invariants
|
|
33
33
|
|
|
@@ -112,6 +112,15 @@
|
|
|
112
112
|
if (isVisibleRegistered) ctx.unregisterVisibleItem(id);
|
|
113
113
|
});
|
|
114
114
|
|
|
115
|
+
function handleHoverStart(itemId: string | number) {
|
|
116
|
+
// The pointer takes the virtual focus, the same as in a ComboBox. Without
|
|
117
|
+
// this the keys move one row and the pointer marks another, and both read
|
|
118
|
+
// as the current one at the same time — which the item cannot correct on
|
|
119
|
+
// its own, because it only sees its own attributes.
|
|
120
|
+
ctx.setFocusVisible(false);
|
|
121
|
+
ctx.setFocusedItemId(itemId);
|
|
122
|
+
}
|
|
123
|
+
|
|
115
124
|
function handleSelect(itemId: string | number) {
|
|
116
125
|
if (ctx.isReadOnly) return;
|
|
117
126
|
// Selection here is pointer-driven (click); clear the keyboard focus ring.
|
|
@@ -136,6 +145,7 @@
|
|
|
136
145
|
isFocusVisibleOverride={isFocusVisible}
|
|
137
146
|
onItemSelect={handleSelect}
|
|
138
147
|
onResolvedTextValue={handleResolvedTextValue}
|
|
148
|
+
onItemHoverStart={handleHoverStart}
|
|
139
149
|
scrollOnFocus={true}
|
|
140
150
|
isParentDisabled={ctx.isDisabled}
|
|
141
151
|
/>
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Avatar
|
|
2
|
+
|
|
3
|
+
## Description
|
|
4
|
+
|
|
5
|
+
`Avatar` is a picture of a person or a thing, with a fallback for when the picture is not there. The image loads off the screen first, and it shows once it is there: a broken image icon never shows. The fallback shows in its place, at once or after a delay.
|
|
6
|
+
|
|
7
|
+
## Anatomy
|
|
8
|
+
|
|
9
|
+
- `Avatar.Root`
|
|
10
|
+
- `Avatar.Image`
|
|
11
|
+
- `Avatar.Fallback`
|
|
12
|
+
- `Avatar.Group`
|
|
13
|
+
- `Avatar.Count`
|
|
14
|
+
|
|
15
|
+
```svelte
|
|
16
|
+
<Avatar.Root class="size-10 rounded-full">
|
|
17
|
+
<Avatar.Image src="/ada.jpg" alt="Ada Lovelace" />
|
|
18
|
+
<Avatar.Fallback delay={600}>AL</Avatar.Fallback>
|
|
19
|
+
</Avatar.Root>
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage guidelines
|
|
23
|
+
|
|
24
|
+
- Give `alt` the name of the person or the thing. Give an empty `alt` when the name is beside the avatar already. The fallback follows the `alt`: it takes the name, or it hides.
|
|
25
|
+
- Give `delay` to the fallback, such as 600, thus a fast image does not flash initials first.
|
|
26
|
+
- Style the root by `data-status`: `loading`, `loaded` or `error`.
|
|
27
|
+
- Read the status with `onStatusChange` on the root.
|
|
28
|
+
- Give `loading="lazy"` to the image on a long list: the load starts when the avatar comes into view.
|
|
29
|
+
- Put a row of avatars in `Avatar.Group` with an `aria-label` and a `max`, and an `Avatar.Count` for the ones past the limit.
|
|
30
|
+
|
|
31
|
+
## API reference
|
|
32
|
+
|
|
33
|
+
- `Avatar.Root`
|
|
34
|
+
- `onStatusChange?: (status: 'loading' | 'loaded' | 'error') => void`
|
|
35
|
+
- `element?: HTMLSpanElement | null` (bindable)
|
|
36
|
+
- `Avatar.Image`
|
|
37
|
+
- `src?: string | null`, `alt: string`
|
|
38
|
+
- `element?: HTMLImageElement | null` (bindable)
|
|
39
|
+
- `Avatar.Fallback`
|
|
40
|
+
- `delay?: number` (`0`)
|
|
41
|
+
- `element?: HTMLSpanElement | null` (bindable)
|
|
42
|
+
- `Avatar.Group`
|
|
43
|
+
- `max?: number`, `aria-label?: string`
|
|
44
|
+
- `Avatar.Count`
|
|
45
|
+
- `children?: Snippet<[{ overflow, count, max }]>`, `aria-label?: string` ("N more", localized)
|
|
46
|
+
|
|
47
|
+
## Accessibility
|
|
48
|
+
|
|
49
|
+
- The image is an `<img>` with your `alt`. The root has no role of its own.
|
|
50
|
+
- The fallback takes the name of the image: it is a `role="img"` named with the `alt`. A screen reader hears the name, and not the letters of the initials. An empty `alt` hides it. Without an image, it is plain content.
|
|
51
|
+
- `Avatar.Group` is a `role="group"` named with your `aria-label`. `Avatar.Count` is a `role="img"` named "N more".
|
|
52
|
+
- Nothing in the avatar is focusable.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { readable } from 'svelte/store';
|
|
3
|
+
import { resolveLocalizedString } from '../../internal/localized-strings';
|
|
4
|
+
import { useLocaleContextOptional } from '../../locale-provider/context';
|
|
5
|
+
import type { AvatarCountProps } from '../types.js';
|
|
6
|
+
import { useAvatarGroupContext } from '../group/context';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Avatar.Count — the count of avatars past the `max` of the group, such as `+3`.
|
|
10
|
+
*
|
|
11
|
+
* It renders only while the group has more avatars than it shows. The screen reader hears
|
|
12
|
+
* "3 more", in the locale of `LocaleProvider`, and not "plus three".
|
|
13
|
+
*/
|
|
14
|
+
let {
|
|
15
|
+
children,
|
|
16
|
+
class: className = '',
|
|
17
|
+
'aria-label': ariaLabelProp,
|
|
18
|
+
element = $bindable<HTMLSpanElement | null>(null),
|
|
19
|
+
...restProps
|
|
20
|
+
}: AvatarCountProps = $props();
|
|
21
|
+
|
|
22
|
+
const group = useAvatarGroupContext('Avatar.Count');
|
|
23
|
+
|
|
24
|
+
const localeContext = useLocaleContextOptional();
|
|
25
|
+
const emptyLocaleStore = readable<string | undefined>(undefined);
|
|
26
|
+
const localeStore = localeContext?.locale ?? emptyLocaleStore;
|
|
27
|
+
const ariaLabel = $derived(
|
|
28
|
+
ariaLabelProp ?? resolveLocalizedString($localeStore, 'avatar.more', { count: group.overflow })
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
let countRef: HTMLSpanElement | null = $state(null);
|
|
32
|
+
|
|
33
|
+
$effect(() => {
|
|
34
|
+
element = countRef;
|
|
35
|
+
return () => {
|
|
36
|
+
element = null;
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
</script>
|
|
40
|
+
|
|
41
|
+
{#if group.overflow > 0}
|
|
42
|
+
<span
|
|
43
|
+
{...restProps}
|
|
44
|
+
bind:this={countRef}
|
|
45
|
+
role="img"
|
|
46
|
+
aria-label={ariaLabel}
|
|
47
|
+
class={className}
|
|
48
|
+
data-avatar-count="true"
|
|
49
|
+
>
|
|
50
|
+
{#if children}
|
|
51
|
+
{@render children({ overflow: group.overflow, count: group.count, max: group.max })}
|
|
52
|
+
{:else}
|
|
53
|
+
+{group.overflow}
|
|
54
|
+
{/if}
|
|
55
|
+
</span>
|
|
56
|
+
{/if}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { AvatarFallbackProps } from '../types.js';
|
|
3
|
+
import { useAvatarContext } from '../root/context';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Avatar.Fallback — what shows in place of the image: initials, an icon, a color.
|
|
7
|
+
*
|
|
8
|
+
* It shows while the image is not on the screen. `delay` holds it back while the image
|
|
9
|
+
* loads, thus a fast image does not flash a fallback first. It shows at once when the image
|
|
10
|
+
* fails, or when there is no image.
|
|
11
|
+
*
|
|
12
|
+
* It takes the name of the image: with an `alt` on `Avatar.Image`, the fallback is a
|
|
13
|
+
* `role="img"` with that `alt` as its name, thus a screen reader hears "Ada Lovelace" and not
|
|
14
|
+
* the letters of her initials. An empty `alt` makes the fallback decorative, the same as it
|
|
15
|
+
* makes the image.
|
|
16
|
+
*/
|
|
17
|
+
let {
|
|
18
|
+
children,
|
|
19
|
+
class: className = '',
|
|
20
|
+
delay = 0,
|
|
21
|
+
element = $bindable<HTMLSpanElement | null>(null),
|
|
22
|
+
...restProps
|
|
23
|
+
}: AvatarFallbackProps = $props();
|
|
24
|
+
|
|
25
|
+
const avatar = useAvatarContext('Avatar.Fallback');
|
|
26
|
+
|
|
27
|
+
let fallbackRef: HTMLSpanElement | null = $state(null);
|
|
28
|
+
// True once `delay` has passed since the load started. It resets with each load.
|
|
29
|
+
let delayPassed = $state(false);
|
|
30
|
+
|
|
31
|
+
$effect(() => {
|
|
32
|
+
element = fallbackRef;
|
|
33
|
+
return () => {
|
|
34
|
+
element = null;
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
$effect(() => {
|
|
39
|
+
if (avatar.status !== 'loading' || delay <= 0) return;
|
|
40
|
+
delayPassed = false;
|
|
41
|
+
const timer = setTimeout(() => {
|
|
42
|
+
delayPassed = true;
|
|
43
|
+
}, delay);
|
|
44
|
+
return () => clearTimeout(timer);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const shown = $derived(
|
|
48
|
+
avatar.status === 'error' || (avatar.status === 'loading' && (delay <= 0 || delayPassed))
|
|
49
|
+
);
|
|
50
|
+
// `undefined` without an image: the fallback is then the content, with no name of its own.
|
|
51
|
+
const name = $derived(avatar.alt);
|
|
52
|
+
</script>
|
|
53
|
+
|
|
54
|
+
{#if shown}
|
|
55
|
+
<span
|
|
56
|
+
{...restProps}
|
|
57
|
+
bind:this={fallbackRef}
|
|
58
|
+
role={name ? 'img' : undefined}
|
|
59
|
+
aria-label={name || undefined}
|
|
60
|
+
aria-hidden={name === '' ? 'true' : undefined}
|
|
61
|
+
class={className}
|
|
62
|
+
data-avatar-fallback="true"
|
|
63
|
+
data-status={avatar.status}
|
|
64
|
+
>
|
|
65
|
+
{@render children?.()}
|
|
66
|
+
</span>
|
|
67
|
+
{/if}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { AvatarGroupProps } from '../types.js';
|
|
3
|
+
import { setAvatarGroupContext, type AvatarGroupContext } from './context';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Avatar.Group — a row of avatars, such as the people on a task.
|
|
7
|
+
*
|
|
8
|
+
* It is a `role="group"`: give it a name with `aria-label`, thus a screen reader user knows
|
|
9
|
+
* what the row is. `max` limits the avatars on the screen. The ones past it render nothing,
|
|
10
|
+
* and `Avatar.Count` says how many they are.
|
|
11
|
+
*/
|
|
12
|
+
let {
|
|
13
|
+
children,
|
|
14
|
+
class: className = '',
|
|
15
|
+
max,
|
|
16
|
+
element = $bindable<HTMLDivElement | null>(null),
|
|
17
|
+
...restProps
|
|
18
|
+
}: AvatarGroupProps = $props();
|
|
19
|
+
|
|
20
|
+
let groupRef: HTMLDivElement | null = $state(null);
|
|
21
|
+
let ids = $state<string[]>([]);
|
|
22
|
+
|
|
23
|
+
const limit = $derived(max === undefined || max < 0 ? Infinity : Math.floor(max));
|
|
24
|
+
|
|
25
|
+
const context: AvatarGroupContext = {
|
|
26
|
+
get count() {
|
|
27
|
+
return ids.length;
|
|
28
|
+
},
|
|
29
|
+
get max() {
|
|
30
|
+
return limit;
|
|
31
|
+
},
|
|
32
|
+
get overflow() {
|
|
33
|
+
return Math.max(0, ids.length - limit);
|
|
34
|
+
},
|
|
35
|
+
register(id) {
|
|
36
|
+
ids = [...ids, id];
|
|
37
|
+
return () => {
|
|
38
|
+
ids = ids.filter((candidate) => candidate !== id);
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
indexOf(id) {
|
|
42
|
+
return ids.indexOf(id);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
setAvatarGroupContext(context);
|
|
47
|
+
|
|
48
|
+
$effect(() => {
|
|
49
|
+
element = groupRef;
|
|
50
|
+
return () => {
|
|
51
|
+
element = null;
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
</script>
|
|
55
|
+
|
|
56
|
+
<div
|
|
57
|
+
{...restProps}
|
|
58
|
+
bind:this={groupRef}
|
|
59
|
+
role="group"
|
|
60
|
+
class={className}
|
|
61
|
+
data-avatar-group="true"
|
|
62
|
+
data-overflow={context.overflow > 0 || undefined}
|
|
63
|
+
>
|
|
64
|
+
{@render children?.()}
|
|
65
|
+
</div>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type AvatarGroupContext = {
|
|
2
|
+
/** The count of avatars in the group, on the screen or not. */
|
|
3
|
+
readonly count: number;
|
|
4
|
+
/** The count of avatars on the screen. `Infinity` without a limit. */
|
|
5
|
+
readonly max: number;
|
|
6
|
+
/** The count of avatars past the limit. */
|
|
7
|
+
readonly overflow: number;
|
|
8
|
+
/** Registers an avatar, and answers its place in the group. The returned function unregisters it. */
|
|
9
|
+
register: (id: string) => () => void;
|
|
10
|
+
/** The place of an avatar in the group, or -1 when it is not registered. */
|
|
11
|
+
indexOf: (id: string) => number;
|
|
12
|
+
};
|
|
13
|
+
export declare function setAvatarGroupContext(context: AvatarGroupContext): void;
|
|
14
|
+
export declare function getAvatarGroupContext(): AvatarGroupContext | undefined;
|
|
15
|
+
export declare function useAvatarGroupContext(part?: string): AvatarGroupContext;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getContext, setContext } from 'svelte';
|
|
2
|
+
const KEY = Symbol('avatar-group');
|
|
3
|
+
export function setAvatarGroupContext(context) {
|
|
4
|
+
setContext(KEY, context);
|
|
5
|
+
}
|
|
6
|
+
export function getAvatarGroupContext() {
|
|
7
|
+
return getContext(KEY);
|
|
8
|
+
}
|
|
9
|
+
export function useAvatarGroupContext(part = 'Avatar') {
|
|
10
|
+
const context = getAvatarGroupContext();
|
|
11
|
+
if (!context) {
|
|
12
|
+
throw new Error(`${part} must be used within Avatar.Group.`);
|
|
13
|
+
}
|
|
14
|
+
return context;
|
|
15
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { untrack } from 'svelte';
|
|
3
|
+
import type { AvatarImageProps } from '../types.js';
|
|
4
|
+
import { useAvatarContext } from '../root/context';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Avatar.Image — the picture.
|
|
8
|
+
*
|
|
9
|
+
* It loads the image off the screen first, and it renders the `<img>` once the image is
|
|
10
|
+
* there: a broken image icon, or a blank box, never shows. Without a `src`, or when the
|
|
11
|
+
* image fails, the status is `error` and `Avatar.Fallback` shows in its place. With
|
|
12
|
+
* `loading="lazy"`, the load waits for the root to come into view.
|
|
13
|
+
*/
|
|
14
|
+
let {
|
|
15
|
+
src,
|
|
16
|
+
alt,
|
|
17
|
+
loading = 'eager',
|
|
18
|
+
class: className = '',
|
|
19
|
+
element = $bindable<HTMLImageElement | null>(null),
|
|
20
|
+
crossorigin,
|
|
21
|
+
referrerpolicy,
|
|
22
|
+
...restProps
|
|
23
|
+
}: AvatarImageProps = $props();
|
|
24
|
+
|
|
25
|
+
const avatar = useAvatarContext('Avatar.Image');
|
|
26
|
+
|
|
27
|
+
let imageRef: HTMLImageElement | null = $state(null);
|
|
28
|
+
|
|
29
|
+
// The fallback takes the name of the image, thus a failed image is not a nameless avatar.
|
|
30
|
+
$effect(() => {
|
|
31
|
+
const name = alt;
|
|
32
|
+
untrack(() => avatar.setAlt(name));
|
|
33
|
+
return () => avatar.setAlt(undefined);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
$effect(() => {
|
|
37
|
+
element = imageRef;
|
|
38
|
+
return () => {
|
|
39
|
+
element = null;
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// A lazy image waits for the root to come into view. Without an observer, it loads at once.
|
|
44
|
+
let inView = $state(false);
|
|
45
|
+
$effect(() => {
|
|
46
|
+
const root = avatar.element;
|
|
47
|
+
if (loading !== 'lazy' || !root || typeof IntersectionObserver === 'undefined') {
|
|
48
|
+
inView = true;
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
inView = false;
|
|
52
|
+
const observer = new IntersectionObserver((entries) => {
|
|
53
|
+
if (entries.some((entry) => entry.isIntersecting)) {
|
|
54
|
+
inView = true;
|
|
55
|
+
observer.disconnect();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
observer.observe(root);
|
|
59
|
+
return () => observer.disconnect();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// The status is set in `untrack`: the setter reads the status to skip a repeat, and a read
|
|
63
|
+
// here would make the load depend on its own result.
|
|
64
|
+
$effect(() => {
|
|
65
|
+
const source = src;
|
|
66
|
+
if (!source) {
|
|
67
|
+
untrack(() => avatar.setStatus('error'));
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (!inView) {
|
|
71
|
+
untrack(() => avatar.setStatus('loading'));
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
// The same request the `<img>` makes, thus the browser serves the `<img>` from its cache.
|
|
75
|
+
const loader = new Image();
|
|
76
|
+
if (crossorigin !== undefined && crossorigin !== null) loader.crossOrigin = crossorigin;
|
|
77
|
+
if (referrerpolicy) loader.referrerPolicy = referrerpolicy;
|
|
78
|
+
loader.onload = () => avatar.setStatus('loaded');
|
|
79
|
+
loader.onerror = () => avatar.setStatus('error');
|
|
80
|
+
loader.src = source;
|
|
81
|
+
// An image from the cache is complete at once: no `loading` frame for the fallback.
|
|
82
|
+
untrack(() => {
|
|
83
|
+
if (loader.complete && loader.naturalWidth > 0) avatar.setStatus('loaded');
|
|
84
|
+
else avatar.setStatus('loading');
|
|
85
|
+
});
|
|
86
|
+
return () => {
|
|
87
|
+
loader.onload = null;
|
|
88
|
+
loader.onerror = null;
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
</script>
|
|
92
|
+
|
|
93
|
+
{#if avatar.status === 'loaded' && src}
|
|
94
|
+
<img
|
|
95
|
+
{...restProps}
|
|
96
|
+
bind:this={imageRef}
|
|
97
|
+
{src}
|
|
98
|
+
{alt}
|
|
99
|
+
{crossorigin}
|
|
100
|
+
{referrerpolicy}
|
|
101
|
+
class={className}
|
|
102
|
+
data-avatar-image="true"
|
|
103
|
+
/>
|
|
104
|
+
{/if}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ComponentProps } from 'svelte';
|
|
2
|
+
import type AvatarCountComponent from './count/avatar-count.svelte';
|
|
3
|
+
import type AvatarFallbackComponent from './fallback/avatar-fallback.svelte';
|
|
4
|
+
import type AvatarGroupComponent from './group/avatar-group.svelte';
|
|
5
|
+
import type AvatarImageComponent from './image/avatar-image.svelte';
|
|
6
|
+
import type AvatarRootComponent from './root/avatar-root.svelte';
|
|
7
|
+
export * as Avatar from './index.parts.js';
|
|
8
|
+
export { default as AvatarRoot } from './root/avatar-root.svelte';
|
|
9
|
+
export { default as AvatarImage } from './image/avatar-image.svelte';
|
|
10
|
+
export { default as AvatarFallback } from './fallback/avatar-fallback.svelte';
|
|
11
|
+
export { default as AvatarGroup } from './group/avatar-group.svelte';
|
|
12
|
+
export { default as AvatarCount } from './count/avatar-count.svelte';
|
|
13
|
+
export type AvatarRootProps = ComponentProps<typeof AvatarRootComponent>;
|
|
14
|
+
export type AvatarImageProps = ComponentProps<typeof AvatarImageComponent>;
|
|
15
|
+
export type AvatarFallbackProps = ComponentProps<typeof AvatarFallbackComponent>;
|
|
16
|
+
export type AvatarGroupProps = ComponentProps<typeof AvatarGroupComponent>;
|
|
17
|
+
export type AvatarCountProps = ComponentProps<typeof AvatarCountComponent>;
|
|
18
|
+
export type { AvatarCountState } from './types.js';
|
|
19
|
+
export { getAvatarGroupContext, setAvatarGroupContext, useAvatarGroupContext, type AvatarGroupContext } from './group/context.js';
|
|
20
|
+
export { getAvatarContext, setAvatarContext, useAvatarContext, type AvatarContext, type AvatarStatus } from './root/context.js';
|
|
21
|
+
import * as AvatarParts from './index.parts.js';
|
|
22
|
+
export default AvatarParts;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * as Avatar from './index.parts.js';
|
|
2
|
+
export { default as AvatarRoot } from './root/avatar-root.svelte';
|
|
3
|
+
export { default as AvatarImage } from './image/avatar-image.svelte';
|
|
4
|
+
export { default as AvatarFallback } from './fallback/avatar-fallback.svelte';
|
|
5
|
+
export { default as AvatarGroup } from './group/avatar-group.svelte';
|
|
6
|
+
export { default as AvatarCount } from './count/avatar-count.svelte';
|
|
7
|
+
export { getAvatarGroupContext, setAvatarGroupContext, useAvatarGroupContext } from './group/context.js';
|
|
8
|
+
export { getAvatarContext, setAvatarContext, useAvatarContext } from './root/context.js';
|
|
9
|
+
import * as AvatarParts from './index.parts.js';
|
|
10
|
+
export default AvatarParts;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { default as Root } from './root/avatar-root.svelte';
|
|
2
|
+
export { default as Image } from './image/avatar-image.svelte';
|
|
3
|
+
export { default as Fallback } from './fallback/avatar-fallback.svelte';
|
|
4
|
+
export { default as Group } from './group/avatar-group.svelte';
|
|
5
|
+
export { default as Count } from './count/avatar-count.svelte';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { default as Root } from './root/avatar-root.svelte';
|
|
2
|
+
export { default as Image } from './image/avatar-image.svelte';
|
|
3
|
+
export { default as Fallback } from './fallback/avatar-fallback.svelte';
|
|
4
|
+
export { default as Group } from './group/avatar-group.svelte';
|
|
5
|
+
export { default as Count } from './count/avatar-count.svelte';
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { AvatarRootProps } from '../types.js';
|
|
3
|
+
import { getAvatarGroupContext } from '../group/context';
|
|
4
|
+
import { setAvatarContext, type AvatarContext, type AvatarStatus } from './context';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Avatar.Root — a picture of a person or a thing, with a fallback for when the picture is
|
|
8
|
+
* not there.
|
|
9
|
+
*
|
|
10
|
+
* It holds the status of the image and gives it to the parts: `Avatar.Image` shows once the
|
|
11
|
+
* image is on the screen, and `Avatar.Fallback` shows while it is not. The root is a plain
|
|
12
|
+
* `<span>` with no role: the name is on the image as `alt`, or on the fallback as text.
|
|
13
|
+
*
|
|
14
|
+
* In an `Avatar.Group`, it takes a place in the order, and it renders nothing past the
|
|
15
|
+
* `max` of the group: a hidden avatar loads no image.
|
|
16
|
+
*/
|
|
17
|
+
let {
|
|
18
|
+
children,
|
|
19
|
+
class: className = '',
|
|
20
|
+
element = $bindable<HTMLSpanElement | null>(null),
|
|
21
|
+
onStatusChange,
|
|
22
|
+
...restProps
|
|
23
|
+
}: AvatarRootProps = $props();
|
|
24
|
+
|
|
25
|
+
let rootRef: HTMLSpanElement | null = $state(null);
|
|
26
|
+
let status = $state<AvatarStatus>('loading');
|
|
27
|
+
let alt = $state<string | undefined>(undefined);
|
|
28
|
+
|
|
29
|
+
// Registered at creation, not in an effect: the place in the group must be known before the
|
|
30
|
+
// first render, or an avatar past the limit would paint once and go.
|
|
31
|
+
const group = getAvatarGroupContext();
|
|
32
|
+
const groupId = $props.id();
|
|
33
|
+
const unregister = group?.register(groupId);
|
|
34
|
+
$effect(() => () => unregister?.());
|
|
35
|
+
const index = $derived(group ? group.indexOf(groupId) : -1);
|
|
36
|
+
const hidden = $derived(group !== undefined && index >= group.max);
|
|
37
|
+
|
|
38
|
+
const context: AvatarContext = {
|
|
39
|
+
get status() {
|
|
40
|
+
return status;
|
|
41
|
+
},
|
|
42
|
+
setStatus(next) {
|
|
43
|
+
if (status === next) return;
|
|
44
|
+
status = next;
|
|
45
|
+
onStatusChange?.(next);
|
|
46
|
+
},
|
|
47
|
+
get alt() {
|
|
48
|
+
return alt;
|
|
49
|
+
},
|
|
50
|
+
setAlt(next) {
|
|
51
|
+
alt = next;
|
|
52
|
+
},
|
|
53
|
+
get element() {
|
|
54
|
+
return rootRef;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
setAvatarContext(context);
|
|
59
|
+
|
|
60
|
+
$effect(() => {
|
|
61
|
+
element = rootRef;
|
|
62
|
+
return () => {
|
|
63
|
+
element = null;
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
</script>
|
|
67
|
+
|
|
68
|
+
{#if !hidden}
|
|
69
|
+
<span
|
|
70
|
+
{...restProps}
|
|
71
|
+
bind:this={rootRef}
|
|
72
|
+
class={className}
|
|
73
|
+
data-avatar-root="true"
|
|
74
|
+
data-status={status}
|
|
75
|
+
data-index={index < 0 ? undefined : index}
|
|
76
|
+
>
|
|
77
|
+
{@render children?.()}
|
|
78
|
+
</span>
|
|
79
|
+
{/if}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** `loading` until the image answers, then `loaded` or `error`. `error` without a `src` too. */
|
|
2
|
+
export type AvatarStatus = 'loading' | 'loaded' | 'error';
|
|
3
|
+
export type AvatarContext = {
|
|
4
|
+
status: AvatarStatus;
|
|
5
|
+
setStatus: (status: AvatarStatus) => void;
|
|
6
|
+
/** The `alt` of the image, thus the fallback can take its name. `undefined` without an image. */
|
|
7
|
+
alt: string | undefined;
|
|
8
|
+
setAlt: (alt: string | undefined) => void;
|
|
9
|
+
/** The root element, for a lazy image that waits for it to come into view. */
|
|
10
|
+
readonly element: HTMLElement | null;
|
|
11
|
+
};
|
|
12
|
+
export declare function setAvatarContext(context: AvatarContext): void;
|
|
13
|
+
export declare function getAvatarContext(): AvatarContext | undefined;
|
|
14
|
+
export declare function useAvatarContext(part?: string): AvatarContext;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getContext, setContext } from 'svelte';
|
|
2
|
+
const KEY = Symbol('avatar');
|
|
3
|
+
export function setAvatarContext(context) {
|
|
4
|
+
setContext(KEY, context);
|
|
5
|
+
}
|
|
6
|
+
export function getAvatarContext() {
|
|
7
|
+
return getContext(KEY);
|
|
8
|
+
}
|
|
9
|
+
export function useAvatarContext(part = 'Avatar') {
|
|
10
|
+
const context = getAvatarContext();
|
|
11
|
+
if (!context) {
|
|
12
|
+
throw new Error(`${part} must be used within Avatar.Root.`);
|
|
13
|
+
}
|
|
14
|
+
return context;
|
|
15
|
+
}
|