@dillingerstaffing/strand-svelte 0.15.2 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/StarRating/StarRating.svelte.d.ts +1 -0
- package/dist/components/StarRating/index.d.ts +3 -0
- package/dist/components/StarRating/index.d.ts.map +1 -0
- package/dist/css/strand-ui.css +200 -11
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1619 -1560
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/components/Nav/Nav.svelte +20 -0
- package/src/components/StarRating/StarRating.svelte +79 -0
- package/src/components/StarRating/StarRating.test.ts +101 -0
- package/src/components/StarRating/index.ts +2 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dillingerstaffing/strand-svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "Strand UI - Svelte component library built on the Strand Design Language",
|
|
5
5
|
"author": "Dillinger Staffing <engineering@dillingerstaffing.com> (https://dillingerstaffing.com)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"svelte": "^4.0.0 || ^5.0.0"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@dillingerstaffing/strand": "^0.
|
|
59
|
+
"@dillingerstaffing/strand": "^0.16.0"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
```
|
|
20
20
|
-->
|
|
21
21
|
<script lang="ts">
|
|
22
|
+
import { onMount, onDestroy } from 'svelte'
|
|
23
|
+
|
|
22
24
|
export interface NavItem {
|
|
23
25
|
label: string
|
|
24
26
|
href: string
|
|
@@ -36,6 +38,24 @@
|
|
|
36
38
|
menuOpen = !menuOpen
|
|
37
39
|
}
|
|
38
40
|
|
|
41
|
+
function syncGlassClass(isGlass: boolean) {
|
|
42
|
+
if (typeof document !== 'undefined') {
|
|
43
|
+
if (isGlass) {
|
|
44
|
+
document.body.classList.add('strand-glass-nav-active')
|
|
45
|
+
} else {
|
|
46
|
+
document.body.classList.remove('strand-glass-nav-active')
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
onMount(() => syncGlassClass(glass))
|
|
52
|
+
onDestroy(() => {
|
|
53
|
+
if (typeof document !== 'undefined') {
|
|
54
|
+
document.body.classList.remove('strand-glass-nav-active')
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
$: syncGlassClass(glass)
|
|
58
|
+
|
|
39
59
|
$: navClasses = ['strand-nav', glass && 'strand-nav--glass'].filter(Boolean).join(' ')
|
|
40
60
|
</script>
|
|
41
61
|
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
<!--! Strand Svelte | MIT License | dillingerstaffing.com -->
|
|
2
|
+
<!--
|
|
3
|
+
Interactive 1-to-5 star rating control. Mirrors the Preact and Vue
|
|
4
|
+
StarRating APIs for cross-consumer parity.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```svelte
|
|
8
|
+
<script>
|
|
9
|
+
import { StarRating } from '@dillingerstaffing/strand-svelte';
|
|
10
|
+
let value = 0;
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<StarRating
|
|
14
|
+
{value}
|
|
15
|
+
onChange={(v) => value = v}
|
|
16
|
+
ariaLabel="Rate this event"
|
|
17
|
+
size="md"
|
|
18
|
+
/>
|
|
19
|
+
```
|
|
20
|
+
-->
|
|
21
|
+
<script lang="ts">
|
|
22
|
+
export let value: number = 0
|
|
23
|
+
export let onChange: ((v: number) => void) | undefined = undefined
|
|
24
|
+
export let size: 'sm' | 'md' | 'lg' = 'md'
|
|
25
|
+
export let readOnly: boolean = false
|
|
26
|
+
export let ariaLabel: string
|
|
27
|
+
|
|
28
|
+
let hover: number = 0
|
|
29
|
+
|
|
30
|
+
$: classes = [
|
|
31
|
+
'strand-star-rating',
|
|
32
|
+
`strand-star-rating--${size}`,
|
|
33
|
+
readOnly && 'strand-star-rating--readonly',
|
|
34
|
+
].filter(Boolean).join(' ')
|
|
35
|
+
|
|
36
|
+
$: display = hover || value
|
|
37
|
+
|
|
38
|
+
function select(n: number): void {
|
|
39
|
+
if (readOnly) return
|
|
40
|
+
onChange?.(n)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function onKey(e: KeyboardEvent, n: number): void {
|
|
44
|
+
if (readOnly) return
|
|
45
|
+
if (e.key === ' ' || e.key === 'Enter') {
|
|
46
|
+
e.preventDefault()
|
|
47
|
+
select(n)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
</script>
|
|
51
|
+
|
|
52
|
+
<div
|
|
53
|
+
class={classes}
|
|
54
|
+
role="radiogroup"
|
|
55
|
+
aria-label={ariaLabel}
|
|
56
|
+
data-strand-component="star-rating"
|
|
57
|
+
data-value={String(value)}
|
|
58
|
+
>
|
|
59
|
+
{#each [1, 2, 3, 4, 5] as n (n)}
|
|
60
|
+
<button
|
|
61
|
+
type="button"
|
|
62
|
+
class={`strand-star-rating__star${n <= display ? ' strand-star-rating__star--active' : ''}`}
|
|
63
|
+
role="radio"
|
|
64
|
+
aria-checked={n === value ? 'true' : 'false'}
|
|
65
|
+
aria-label={`${n} star${n > 1 ? 's' : ''}`}
|
|
66
|
+
tabindex={readOnly ? -1 : 0}
|
|
67
|
+
disabled={readOnly}
|
|
68
|
+
data-star-value={String(n)}
|
|
69
|
+
on:click={() => select(n)}
|
|
70
|
+
on:keydown={(e) => onKey(e, n)}
|
|
71
|
+
on:mouseenter={readOnly ? undefined : () => (hover = n)}
|
|
72
|
+
on:mouseleave={readOnly ? undefined : () => (hover = 0)}
|
|
73
|
+
on:focus={readOnly ? undefined : () => (hover = n)}
|
|
74
|
+
on:blur={readOnly ? undefined : () => (hover = 0)}
|
|
75
|
+
>
|
|
76
|
+
<span class="strand-star-rating__glyph" aria-hidden="true">{'\u2605'}</span>
|
|
77
|
+
</button>
|
|
78
|
+
{/each}
|
|
79
|
+
</div>
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/*! Strand Svelte | MIT License | dillingerstaffing.com */
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect, vi } from 'vitest'
|
|
4
|
+
import { render, fireEvent } from '@testing-library/svelte'
|
|
5
|
+
import StarRating from './StarRating.svelte'
|
|
6
|
+
|
|
7
|
+
describe('StarRating', () => {
|
|
8
|
+
it('renders five radio buttons', () => {
|
|
9
|
+
const { container } = render(StarRating, { props: { value: 0, ariaLabel: 'Rate' } })
|
|
10
|
+
expect(container.querySelectorAll('[role="radio"]').length).toBe(5)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('applies the active class to stars up to the value', () => {
|
|
14
|
+
const { container } = render(StarRating, { props: { value: 3, ariaLabel: 'Rate' } })
|
|
15
|
+
expect(
|
|
16
|
+
container.querySelectorAll('.strand-star-rating__star--active').length,
|
|
17
|
+
).toBe(3)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('sets aria-checked true on the selected star', () => {
|
|
21
|
+
const { container } = render(StarRating, { props: { value: 2, ariaLabel: 'Rate' } })
|
|
22
|
+
const radios = container.querySelectorAll('[role="radio"]')
|
|
23
|
+
expect(radios[1]).toHaveAttribute('aria-checked', 'true')
|
|
24
|
+
expect(radios[0]).toHaveAttribute('aria-checked', 'false')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('calls onChange with the clicked star value', async () => {
|
|
28
|
+
const onChange = vi.fn()
|
|
29
|
+
const { container } = render(StarRating, {
|
|
30
|
+
props: { value: 0, ariaLabel: 'Rate', onChange },
|
|
31
|
+
})
|
|
32
|
+
const radios = container.querySelectorAll('[role="radio"]')
|
|
33
|
+
await fireEvent.click(radios[3])
|
|
34
|
+
expect(onChange).toHaveBeenCalledWith(4)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('does not call onChange in readOnly mode', async () => {
|
|
38
|
+
const onChange = vi.fn()
|
|
39
|
+
const { container } = render(StarRating, {
|
|
40
|
+
props: { value: 2, ariaLabel: 'Rate', readOnly: true, onChange },
|
|
41
|
+
})
|
|
42
|
+
const radios = container.querySelectorAll('[role="radio"]')
|
|
43
|
+
await fireEvent.click(radios[4])
|
|
44
|
+
expect(onChange).not.toHaveBeenCalled()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('applies the size modifier class', () => {
|
|
48
|
+
const { container } = render(StarRating, {
|
|
49
|
+
props: { value: 0, ariaLabel: 'Rate', size: 'lg' },
|
|
50
|
+
})
|
|
51
|
+
expect(container.querySelector('.strand-star-rating--lg')).toBeInTheDocument()
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('defaults to md size', () => {
|
|
55
|
+
const { container } = render(StarRating, { props: { value: 0, ariaLabel: 'Rate' } })
|
|
56
|
+
expect(container.querySelector('.strand-star-rating--md')).toBeInTheDocument()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('applies readonly class when readOnly is set', () => {
|
|
60
|
+
const { container } = render(StarRating, {
|
|
61
|
+
props: { value: 3, ariaLabel: 'Rate', readOnly: true },
|
|
62
|
+
})
|
|
63
|
+
expect(
|
|
64
|
+
container.querySelector('.strand-star-rating--readonly'),
|
|
65
|
+
).toBeInTheDocument()
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('exposes data-strand-component attribute', () => {
|
|
69
|
+
const { container } = render(StarRating, { props: { value: 0, ariaLabel: 'Rate' } })
|
|
70
|
+
expect(
|
|
71
|
+
container.querySelector('[data-strand-component="star-rating"]'),
|
|
72
|
+
).toBeInTheDocument()
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('reflects the aria-label prop on the radiogroup', () => {
|
|
76
|
+
const { container } = render(StarRating, {
|
|
77
|
+
props: { value: 0, ariaLabel: 'Rate event' },
|
|
78
|
+
})
|
|
79
|
+
expect(container.querySelector('[aria-label="Rate event"]')).toBeInTheDocument()
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('activates a star on Space keydown', async () => {
|
|
83
|
+
const onChange = vi.fn()
|
|
84
|
+
const { container } = render(StarRating, {
|
|
85
|
+
props: { value: 0, ariaLabel: 'Rate', onChange },
|
|
86
|
+
})
|
|
87
|
+
const radios = container.querySelectorAll('[role="radio"]')
|
|
88
|
+
await fireEvent.keyDown(radios[1], { key: ' ' })
|
|
89
|
+
expect(onChange).toHaveBeenCalledWith(2)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('disables star buttons in read-only mode', () => {
|
|
93
|
+
const { container } = render(StarRating, {
|
|
94
|
+
props: { value: 3, ariaLabel: 'Rate', readOnly: true },
|
|
95
|
+
})
|
|
96
|
+
const radios = container.querySelectorAll('[role="radio"]')
|
|
97
|
+
for (const r of radios) {
|
|
98
|
+
expect(r).toBeDisabled()
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
})
|
package/src/index.ts
CHANGED
|
@@ -46,3 +46,4 @@ export { default as Tooltip } from './components/Tooltip/Tooltip.svelte'
|
|
|
46
46
|
export { default as Progress } from './components/Progress/Progress.svelte'
|
|
47
47
|
export { default as Spinner } from './components/Spinner/Spinner.svelte'
|
|
48
48
|
export { default as Skeleton } from './components/Skeleton/Skeleton.svelte'
|
|
49
|
+
export { default as StarRating } from './components/StarRating/StarRating.svelte'
|