@dillingerstaffing/strand-vue 0.15.1 → 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/README.md +3 -3
- package/dist/components/Nav/Nav.vue.d.ts.map +1 -1
- package/dist/components/StarRating/StarRating.vue.d.ts +13 -0
- package/dist/components/StarRating/StarRating.vue.d.ts.map +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 +231 -16
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +554 -494
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
- package/src/components/Nav/Nav.vue +13 -1
- package/src/components/StarRating/StarRating.test.ts +111 -0
- package/src/components/StarRating/StarRating.vue +97 -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-vue",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "Strand UI - Vue 3 component library built on the Strand Design Language",
|
|
5
5
|
"author": "Dillinger Staffing <engineering@dillingerstaffing.com> (https://dillingerstaffing.com)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -45,14 +45,15 @@
|
|
|
45
45
|
"dist/css/*.css"
|
|
46
46
|
],
|
|
47
47
|
"scripts": {
|
|
48
|
-
"
|
|
48
|
+
"prepack": "pnpm run build",
|
|
49
|
+
"build": "vite build && cp ../../generated/html-reference.md ./HTML_REFERENCE.md && cp ../../docs/design-language.md ./DESIGN_LANGUAGE.md",
|
|
49
50
|
"test": "vitest run"
|
|
50
51
|
},
|
|
51
52
|
"peerDependencies": {
|
|
52
53
|
"vue": "^3.4.0"
|
|
53
54
|
},
|
|
54
55
|
"dependencies": {
|
|
55
|
-
"@dillingerstaffing/strand": "^0.
|
|
56
|
+
"@dillingerstaffing/strand": "^0.16.0"
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
58
59
|
"@testing-library/jest-dom": "^6.6.0",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
```
|
|
24
24
|
-->
|
|
25
25
|
<script setup lang="ts">
|
|
26
|
-
import { computed, ref } from 'vue'
|
|
26
|
+
import { computed, ref, onMounted, onUnmounted, watch } from 'vue'
|
|
27
27
|
|
|
28
28
|
export interface NavItem {
|
|
29
29
|
label: string
|
|
@@ -49,6 +49,18 @@ function toggleMenu() {
|
|
|
49
49
|
menuOpen.value = !menuOpen.value
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
function syncGlassClass(isGlass: boolean) {
|
|
53
|
+
if (isGlass) {
|
|
54
|
+
document.body.classList.add('strand-glass-nav-active')
|
|
55
|
+
} else {
|
|
56
|
+
document.body.classList.remove('strand-glass-nav-active')
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
onMounted(() => syncGlassClass(props.glass))
|
|
61
|
+
onUnmounted(() => document.body.classList.remove('strand-glass-nav-active'))
|
|
62
|
+
watch(() => props.glass, syncGlassClass)
|
|
63
|
+
|
|
52
64
|
const classes = computed(() => ['strand-nav', props.glass && 'strand-nav--glass'].filter(Boolean).join(' '))
|
|
53
65
|
</script>
|
|
54
66
|
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/*! Strand Vue | MIT License | dillingerstaffing.com */
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect, vi } from 'vitest'
|
|
4
|
+
import { render, fireEvent } from '@testing-library/vue'
|
|
5
|
+
import StarRating from './StarRating.vue'
|
|
6
|
+
|
|
7
|
+
describe('StarRating', () => {
|
|
8
|
+
it('renders five radio buttons', () => {
|
|
9
|
+
const { container } = render(StarRating, {
|
|
10
|
+
props: { value: 0, ariaLabel: 'Rate' },
|
|
11
|
+
})
|
|
12
|
+
expect(container.querySelectorAll('[role="radio"]').length).toBe(5)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('applies the active class to stars up to the value', () => {
|
|
16
|
+
const { container } = render(StarRating, {
|
|
17
|
+
props: { value: 3, ariaLabel: 'Rate' },
|
|
18
|
+
})
|
|
19
|
+
expect(
|
|
20
|
+
container.querySelectorAll('.strand-star-rating__star--active').length,
|
|
21
|
+
).toBe(3)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('sets aria-checked true on the selected star', () => {
|
|
25
|
+
const { container } = render(StarRating, {
|
|
26
|
+
props: { value: 2, ariaLabel: 'Rate' },
|
|
27
|
+
})
|
|
28
|
+
const radios = container.querySelectorAll('[role="radio"]')
|
|
29
|
+
expect(radios[1]).toHaveAttribute('aria-checked', 'true')
|
|
30
|
+
expect(radios[0]).toHaveAttribute('aria-checked', 'false')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('calls onChange with the clicked star value', async () => {
|
|
34
|
+
const onChange = vi.fn()
|
|
35
|
+
const { container } = render(StarRating, {
|
|
36
|
+
props: { value: 0, ariaLabel: 'Rate', onChange },
|
|
37
|
+
})
|
|
38
|
+
const radios = container.querySelectorAll('[role="radio"]')
|
|
39
|
+
await fireEvent.click(radios[3])
|
|
40
|
+
expect(onChange).toHaveBeenCalledWith(4)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('does not call onChange in readOnly mode', async () => {
|
|
44
|
+
const onChange = vi.fn()
|
|
45
|
+
const { container } = render(StarRating, {
|
|
46
|
+
props: { value: 2, ariaLabel: 'Rate', readOnly: true, onChange },
|
|
47
|
+
})
|
|
48
|
+
const radios = container.querySelectorAll('[role="radio"]')
|
|
49
|
+
await fireEvent.click(radios[4])
|
|
50
|
+
expect(onChange).not.toHaveBeenCalled()
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('applies the size modifier class', () => {
|
|
54
|
+
const { container } = render(StarRating, {
|
|
55
|
+
props: { value: 0, ariaLabel: 'Rate', size: 'lg' },
|
|
56
|
+
})
|
|
57
|
+
expect(container.querySelector('.strand-star-rating--lg')).toBeInTheDocument()
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('defaults to md size', () => {
|
|
61
|
+
const { container } = render(StarRating, {
|
|
62
|
+
props: { value: 0, ariaLabel: 'Rate' },
|
|
63
|
+
})
|
|
64
|
+
expect(container.querySelector('.strand-star-rating--md')).toBeInTheDocument()
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('applies readonly class when readOnly is set', () => {
|
|
68
|
+
const { container } = render(StarRating, {
|
|
69
|
+
props: { value: 3, ariaLabel: 'Rate', readOnly: true },
|
|
70
|
+
})
|
|
71
|
+
expect(
|
|
72
|
+
container.querySelector('.strand-star-rating--readonly'),
|
|
73
|
+
).toBeInTheDocument()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('exposes data-strand-component attribute', () => {
|
|
77
|
+
const { container } = render(StarRating, {
|
|
78
|
+
props: { value: 0, ariaLabel: 'Rate' },
|
|
79
|
+
})
|
|
80
|
+
expect(
|
|
81
|
+
container.querySelector('[data-strand-component="star-rating"]'),
|
|
82
|
+
).toBeInTheDocument()
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('reflects the aria-label prop on the radiogroup', () => {
|
|
86
|
+
const { container } = render(StarRating, {
|
|
87
|
+
props: { value: 0, ariaLabel: 'Rate event' },
|
|
88
|
+
})
|
|
89
|
+
expect(container.querySelector('[aria-label="Rate event"]')).toBeInTheDocument()
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('activates a star on Space keydown', async () => {
|
|
93
|
+
const onChange = vi.fn()
|
|
94
|
+
const { container } = render(StarRating, {
|
|
95
|
+
props: { value: 0, ariaLabel: 'Rate', onChange },
|
|
96
|
+
})
|
|
97
|
+
const radios = container.querySelectorAll('[role="radio"]')
|
|
98
|
+
await fireEvent.keyDown(radios[1], { key: ' ' })
|
|
99
|
+
expect(onChange).toHaveBeenCalledWith(2)
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('disables star buttons in read-only mode', () => {
|
|
103
|
+
const { container } = render(StarRating, {
|
|
104
|
+
props: { value: 3, ariaLabel: 'Rate', readOnly: true },
|
|
105
|
+
})
|
|
106
|
+
const radios = container.querySelectorAll('[role="radio"]')
|
|
107
|
+
for (const r of radios) {
|
|
108
|
+
expect(r).toBeDisabled()
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
})
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
<!--! Strand Vue | MIT License | dillingerstaffing.com -->
|
|
2
|
+
<!--
|
|
3
|
+
Interactive 1-to-5 star rating control. Mirrors the Preact and Svelte
|
|
4
|
+
StarRating APIs for cross-consumer parity.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```vue
|
|
8
|
+
<script setup>
|
|
9
|
+
import { ref } from 'vue';
|
|
10
|
+
import { StarRating } from '@dillingerstaffing/strand-vue';
|
|
11
|
+
const value = ref(0);
|
|
12
|
+
</script>
|
|
13
|
+
|
|
14
|
+
<template>
|
|
15
|
+
<StarRating
|
|
16
|
+
:value="value"
|
|
17
|
+
:onChange="(v) => value = v"
|
|
18
|
+
ariaLabel="Rate this event"
|
|
19
|
+
size="md"
|
|
20
|
+
/>
|
|
21
|
+
</template>
|
|
22
|
+
```
|
|
23
|
+
-->
|
|
24
|
+
<script setup lang="ts">
|
|
25
|
+
import { computed, ref } from 'vue'
|
|
26
|
+
|
|
27
|
+
export interface StarRatingProps {
|
|
28
|
+
value: number
|
|
29
|
+
onChange?: (v: number) => void
|
|
30
|
+
size?: 'sm' | 'md' | 'lg'
|
|
31
|
+
readOnly?: boolean
|
|
32
|
+
ariaLabel: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const props = withDefaults(defineProps<StarRatingProps>(), {
|
|
36
|
+
size: 'md',
|
|
37
|
+
readOnly: false,
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const hover = ref(0)
|
|
41
|
+
|
|
42
|
+
const classes = computed(() =>
|
|
43
|
+
[
|
|
44
|
+
'strand-star-rating',
|
|
45
|
+
`strand-star-rating--${props.size}`,
|
|
46
|
+
props.readOnly && 'strand-star-rating--readonly',
|
|
47
|
+
]
|
|
48
|
+
.filter(Boolean)
|
|
49
|
+
.join(' '),
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
const display = computed(() => hover.value || props.value)
|
|
53
|
+
|
|
54
|
+
function select(n: number): void {
|
|
55
|
+
if (props.readOnly) return
|
|
56
|
+
props.onChange?.(n)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function onKey(e: KeyboardEvent, n: number): void {
|
|
60
|
+
if (props.readOnly) return
|
|
61
|
+
if (e.key === ' ' || e.key === 'Enter') {
|
|
62
|
+
e.preventDefault()
|
|
63
|
+
select(n)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
</script>
|
|
67
|
+
|
|
68
|
+
<template>
|
|
69
|
+
<div
|
|
70
|
+
:class="classes"
|
|
71
|
+
role="radiogroup"
|
|
72
|
+
:aria-label="ariaLabel"
|
|
73
|
+
data-strand-component="star-rating"
|
|
74
|
+
:data-value="String(value)"
|
|
75
|
+
>
|
|
76
|
+
<button
|
|
77
|
+
v-for="n in 5"
|
|
78
|
+
:key="n"
|
|
79
|
+
type="button"
|
|
80
|
+
:class="`strand-star-rating__star${n <= display ? ' strand-star-rating__star--active' : ''}`"
|
|
81
|
+
role="radio"
|
|
82
|
+
:aria-checked="n === value ? 'true' : 'false'"
|
|
83
|
+
:aria-label="`${n} star${n > 1 ? 's' : ''}`"
|
|
84
|
+
:tabindex="readOnly ? -1 : 0"
|
|
85
|
+
:disabled="readOnly"
|
|
86
|
+
:data-star-value="String(n)"
|
|
87
|
+
@click="select(n)"
|
|
88
|
+
@keydown="(e: KeyboardEvent) => onKey(e, n)"
|
|
89
|
+
@mouseenter="!readOnly && (hover = n)"
|
|
90
|
+
@mouseleave="!readOnly && (hover = 0)"
|
|
91
|
+
@focus="!readOnly && (hover = n)"
|
|
92
|
+
@blur="!readOnly && (hover = 0)"
|
|
93
|
+
>
|
|
94
|
+
<span class="strand-star-rating__glyph" aria-hidden="true">{{ "\u2605" }}</span>
|
|
95
|
+
</button>
|
|
96
|
+
</div>
|
|
97
|
+
</template>
|
package/src/index.ts
CHANGED
|
@@ -45,3 +45,4 @@ export { default as Tooltip } from "./components/Tooltip/Tooltip.vue";
|
|
|
45
45
|
export { default as Progress } from "./components/Progress/Progress.vue";
|
|
46
46
|
export { default as Spinner } from "./components/Spinner/Spinner.vue";
|
|
47
47
|
export { default as Skeleton } from "./components/Skeleton/Skeleton.vue";
|
|
48
|
+
export { default as StarRating } from "./components/StarRating/StarRating.vue";
|