fds-vue-core 9.1.0 → 9.2.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/components.d.ts +4 -0
- package/dist/components/Buttons/FdsButtonIcon/FdsButtonIcon.vue_vue_type_script_setup_true_lang.js +1 -0
- package/dist/components/Buttons/FdsButtonIcon/FdsButtonIcon.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/components/Buttons/FdsButtonIcon/types.d.ts +1 -0
- package/dist/components/FdsSnackbar/FdsSnackbar.js +6 -0
- package/dist/components/FdsSnackbar/FdsSnackbar.js.map +1 -0
- package/dist/components/FdsSnackbar/FdsSnackbar.vue.d.ts +7 -0
- package/dist/components/FdsSnackbar/FdsSnackbar.vue_vue_type_script_setup_true_lang.js +78 -0
- package/dist/components/FdsSnackbar/FdsSnackbar.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/components/FdsSnackbar/types.d.ts +16 -0
- package/dist/components/FdsSnackbar/useSnackbar.d.ts +40 -0
- package/dist/components/FdsSnackbar/useSnackbar.js +19 -0
- package/dist/components/FdsSnackbar/useSnackbar.js.map +1 -0
- package/dist/components/FdsToastMessage/FdsToastMessage.js +6 -0
- package/dist/components/FdsToastMessage/FdsToastMessage.js.map +1 -0
- package/dist/components/FdsToastMessage/FdsToastMessage.vue.d.ts +8 -0
- package/dist/components/FdsToastMessage/FdsToastMessage.vue_vue_type_script_setup_true_lang.js +58 -0
- package/dist/components/FdsToastMessage/FdsToastMessage.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/components/FdsToastMessage/FdsToastMessageCard.js +6 -0
- package/dist/components/FdsToastMessage/FdsToastMessageCard.js.map +1 -0
- package/dist/components/FdsToastMessage/FdsToastMessageCard.vue.d.ts +20 -0
- package/dist/components/FdsToastMessage/FdsToastMessageCard.vue_vue_type_script_setup_true_lang.js +185 -0
- package/dist/components/FdsToastMessage/FdsToastMessageCard.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/components/FdsToastMessage/FdsToastRegions.js +8 -0
- package/dist/components/FdsToastMessage/FdsToastRegions.js.map +1 -0
- package/dist/components/FdsToastMessage/FdsToastRegions.vue.d.ts +28 -0
- package/dist/components/FdsToastMessage/FdsToastRegions.vue_vue_type_script_setup_true_lang.js +81 -0
- package/dist/components/FdsToastMessage/FdsToastRegions.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/components/FdsToastMessage/createMessageQueue.d.ts +19 -0
- package/dist/components/FdsToastMessage/createMessageQueue.js +50 -0
- package/dist/components/FdsToastMessage/createMessageQueue.js.map +1 -0
- package/dist/components/FdsToastMessage/getReadingDuration.d.ts +1 -0
- package/dist/components/FdsToastMessage/getReadingDuration.js +11 -0
- package/dist/components/FdsToastMessage/getReadingDuration.js.map +1 -0
- package/dist/components/FdsToastMessage/types.d.ts +20 -0
- package/dist/components/FdsToastMessage/useToastMessage.d.ts +40 -0
- package/dist/components/FdsToastMessage/useToastMessage.js +19 -0
- package/dist/components/FdsToastMessage/useToastMessage.js.map +1 -0
- package/dist/fds-vue-core.css +2 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +9 -3
- package/dist/index.js.map +1 -1
- package/dist/lang/en.js +6 -0
- package/dist/lang/en.js.map +1 -1
- package/dist/lang/sv.js +6 -0
- package/dist/lang/sv.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +13 -13
- package/src/components/Buttons/FdsButtonIcon/types.ts +1 -0
- package/src/components/FdsSnackbar/FdsSnackbar.stories.ts +111 -0
- package/src/components/FdsSnackbar/FdsSnackbar.vue +74 -0
- package/src/components/FdsSnackbar/types.ts +21 -0
- package/src/components/FdsSnackbar/useSnackbar.ts +19 -0
- package/src/components/FdsToastMessage/FdsToastMessage.stories.ts +122 -0
- package/src/components/FdsToastMessage/FdsToastMessage.vue +44 -0
- package/src/components/FdsToastMessage/FdsToastMessageCard.vue +173 -0
- package/src/components/FdsToastMessage/FdsToastRegions.vue +114 -0
- package/src/components/FdsToastMessage/createMessageQueue.ts +57 -0
- package/src/components/FdsToastMessage/getReadingDuration.ts +8 -0
- package/src/components/FdsToastMessage/types.ts +31 -0
- package/src/components/FdsToastMessage/useToastMessage.ts +19 -0
- package/src/index.ts +27 -0
- package/src/lang/en.json +6 -0
- package/src/lang/sv.json +6 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fds-vue-core",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.2.0",
|
|
4
4
|
"description": "FDS Vue Core Component Library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -46,32 +46,32 @@
|
|
|
46
46
|
"vue-router": "^5.0.0"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"axios": "1.21.
|
|
49
|
+
"axios": "1.21.1",
|
|
50
50
|
"date-fns": "4.4.0",
|
|
51
51
|
"imask": "7.6.1",
|
|
52
|
-
"libphonenumber-js": "1.13.
|
|
52
|
+
"libphonenumber-js": "1.13.16",
|
|
53
53
|
"personnummer": "3.3.1",
|
|
54
54
|
"qrcode.vue": "3.11.0",
|
|
55
|
-
"tailwindcss": "4.3.
|
|
55
|
+
"tailwindcss": "4.3.5"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@chromatic-com/storybook": "5.6.0",
|
|
59
59
|
"@intlify/unplugin-vue-i18n": "11.2.8",
|
|
60
|
-
"@storybook/addon-a11y": "10.6.
|
|
61
|
-
"@storybook/addon-docs": "10.6.
|
|
62
|
-
"@storybook/addon-vitest": "10.6.
|
|
63
|
-
"@storybook/vue3": "10.6.
|
|
64
|
-
"@storybook/vue3-vite": "10.6.
|
|
65
|
-
"@tailwindcss/vite": "4.3.
|
|
60
|
+
"@storybook/addon-a11y": "10.6.7",
|
|
61
|
+
"@storybook/addon-docs": "10.6.7",
|
|
62
|
+
"@storybook/addon-vitest": "10.6.7",
|
|
63
|
+
"@storybook/vue3": "10.6.7",
|
|
64
|
+
"@storybook/vue3-vite": "10.6.7",
|
|
65
|
+
"@tailwindcss/vite": "4.3.5",
|
|
66
66
|
"@types/node": "26.6.6",
|
|
67
67
|
"@vitejs/plugin-vue": "6.0.10",
|
|
68
68
|
"@vitest/browser": "5.1.0",
|
|
69
69
|
"@vitest/browser-playwright": "5.1.0",
|
|
70
70
|
"@vue/language-core": "3.3.14",
|
|
71
|
-
"fg-devkit": "1.29.
|
|
71
|
+
"fg-devkit": "1.29.1",
|
|
72
72
|
"happy-dom": "21.0.4",
|
|
73
|
-
"playwright": "1.
|
|
74
|
-
"storybook": "10.6.
|
|
73
|
+
"playwright": "1.65.0",
|
|
74
|
+
"storybook": "10.6.7",
|
|
75
75
|
"tsx": "4.23.15",
|
|
76
76
|
"vite": "8.3.6",
|
|
77
77
|
"vite-plugin-dts": "5.1.2",
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/vue3'
|
|
2
|
+
import FdsButtonSecondary from '../Buttons/FdsButtonSecondary/FdsButtonSecondary.vue'
|
|
3
|
+
import FdsToastMessageCard from '../FdsToastMessage/FdsToastMessageCard.vue'
|
|
4
|
+
import type { FdsToastMessageType } from '../FdsToastMessage/types'
|
|
5
|
+
import FdsSnackbar from './FdsSnackbar.vue'
|
|
6
|
+
import type { FdsSnackbarProps } from './types'
|
|
7
|
+
import { useSnackbar } from './useSnackbar'
|
|
8
|
+
|
|
9
|
+
type FdsSnackbarStoryArgs = FdsSnackbarProps & { message: string; actionLabel: string }
|
|
10
|
+
|
|
11
|
+
const meta: Meta<FdsSnackbarStoryArgs> = {
|
|
12
|
+
title: 'FDS/FdsSnackbar',
|
|
13
|
+
component: FdsSnackbar,
|
|
14
|
+
tags: ['autodocs'],
|
|
15
|
+
argTypes: {
|
|
16
|
+
position: {
|
|
17
|
+
control: { type: 'select' },
|
|
18
|
+
options: ['top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right'],
|
|
19
|
+
},
|
|
20
|
+
duration: { control: { type: 'number' } },
|
|
21
|
+
message: { control: { type: 'text' } },
|
|
22
|
+
actionLabel: { control: { type: 'text' } },
|
|
23
|
+
},
|
|
24
|
+
args: {
|
|
25
|
+
position: 'bottom-center',
|
|
26
|
+
duration: 10000,
|
|
27
|
+
message: 'This is a snackbar.',
|
|
28
|
+
actionLabel: 'Action',
|
|
29
|
+
},
|
|
30
|
+
parameters: {
|
|
31
|
+
docs: {
|
|
32
|
+
description: {
|
|
33
|
+
component:
|
|
34
|
+
'Place `<FdsSnackbar />` once in App.vue and show messages from anywhere with `useSnackbar()`. One snackbar is shown at a time and the rest wait in a queue. It closes after at least 10 seconds by default (longer texts stay 1 s plus 300 ms per word), when the user clicks the action or closes it.',
|
|
35
|
+
},
|
|
36
|
+
source: {
|
|
37
|
+
code: `<!-- App.vue -->
|
|
38
|
+
<FdsSnackbar />
|
|
39
|
+
|
|
40
|
+
// Anywhere
|
|
41
|
+
const snackbar = useSnackbar()
|
|
42
|
+
snackbar.info('The booking was cancelled', {
|
|
43
|
+
action: { label: 'Action', onClick: () => restoreBooking() },
|
|
44
|
+
})
|
|
45
|
+
snackbar.error('No connection to the server', {
|
|
46
|
+
action: { label: 'Action', onClick: () => reconnect() },
|
|
47
|
+
})`,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default meta
|
|
54
|
+
type Story = StoryObj<typeof meta>
|
|
55
|
+
|
|
56
|
+
export const Default: Story = {
|
|
57
|
+
render: (args) => ({
|
|
58
|
+
components: { FdsSnackbar, FdsButtonSecondary },
|
|
59
|
+
setup() {
|
|
60
|
+
const snackbar = useSnackbar()
|
|
61
|
+
const types: FdsToastMessageType[] = ['success', 'info', 'warning', 'error']
|
|
62
|
+
const showSnackbar = (type: FdsToastMessageType, message = args.message) => {
|
|
63
|
+
snackbar.show({
|
|
64
|
+
type,
|
|
65
|
+
message,
|
|
66
|
+
action: { label: args.actionLabel, onClick: () => showSnackbar('success', 'The action was clicked.') },
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
return { args, types, showSnackbar, clear: snackbar.clear }
|
|
70
|
+
},
|
|
71
|
+
template: `
|
|
72
|
+
<div class="flex flex-wrap gap-2">
|
|
73
|
+
<FdsButtonSecondary v-for="type in types" :key="type" :text="type" @click="showSnackbar(type)" />
|
|
74
|
+
<FdsButtonSecondary text="Clear all" @click="clear" />
|
|
75
|
+
</div>
|
|
76
|
+
<FdsSnackbar :position="args.position" :duration="args.duration" />
|
|
77
|
+
`,
|
|
78
|
+
}),
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const ActionOnAllTypes: Story = {
|
|
82
|
+
render: (args) => ({
|
|
83
|
+
components: { FdsToastMessageCard },
|
|
84
|
+
setup() {
|
|
85
|
+
const types: FdsToastMessageType[] = ['success', 'info', 'warning', 'error']
|
|
86
|
+
return { args, types }
|
|
87
|
+
},
|
|
88
|
+
template: `
|
|
89
|
+
<div class="flex flex-col gap-3 max-w-xl">
|
|
90
|
+
<FdsToastMessageCard
|
|
91
|
+
v-for="type in types"
|
|
92
|
+
:key="type"
|
|
93
|
+
:type="type"
|
|
94
|
+
:message="args.message"
|
|
95
|
+
:duration="0"
|
|
96
|
+
:closeable="true"
|
|
97
|
+
:actionLabel="args.actionLabel"
|
|
98
|
+
/>
|
|
99
|
+
</div>
|
|
100
|
+
`,
|
|
101
|
+
}),
|
|
102
|
+
parameters: {
|
|
103
|
+
docs: {
|
|
104
|
+
source: {
|
|
105
|
+
code: `snackbar.error('The booking could not be saved.', {
|
|
106
|
+
action: { label: 'Try again', onClick: () => save() },
|
|
107
|
+
})`,
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { onBeforeUnmount, onMounted } from 'vue'
|
|
3
|
+
import { useFdsI18n } from '../../plugin/useFdsI18n'
|
|
4
|
+
import FdsToastMessageCard from '../FdsToastMessage/FdsToastMessageCard.vue'
|
|
5
|
+
import FdsToastRegions from '../FdsToastMessage/FdsToastRegions.vue'
|
|
6
|
+
import { getReadingDuration } from '../FdsToastMessage/getReadingDuration'
|
|
7
|
+
import type { FdsSnackbarItem, FdsSnackbarProps } from './types'
|
|
8
|
+
import { useSnackbar, useSnackbarContainer } from './useSnackbar'
|
|
9
|
+
|
|
10
|
+
withDefaults(defineProps<FdsSnackbarProps>(), {
|
|
11
|
+
position: 'bottom-center',
|
|
12
|
+
duration: 10000,
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const { snackbars, dismiss } = useSnackbar()
|
|
16
|
+
const isActiveContainer = useSnackbarContainer()
|
|
17
|
+
const { t } = useFdsI18n()
|
|
18
|
+
|
|
19
|
+
const visibleSnackbarSelector = '.fds-toast:not(.fds-toast-leave-active) [data-fds-snackbar]'
|
|
20
|
+
let focusBeforeSnackbar: HTMLElement | null = null
|
|
21
|
+
|
|
22
|
+
function focusSnackbarAction(event: KeyboardEvent) {
|
|
23
|
+
const isShortcut = event.altKey && event.code === 'KeyT' && !event.ctrlKey && !event.metaKey
|
|
24
|
+
if (!isShortcut || !isActiveContainer.value) return
|
|
25
|
+
const actionButton = document.querySelector<HTMLElement>(`${visibleSnackbarSelector} button`)
|
|
26
|
+
if (!actionButton) return
|
|
27
|
+
event.preventDefault()
|
|
28
|
+
const { activeElement } = document
|
|
29
|
+
if (activeElement instanceof HTMLElement && !activeElement.closest('[data-fds-snackbar]')) {
|
|
30
|
+
focusBeforeSnackbar = activeElement
|
|
31
|
+
}
|
|
32
|
+
actionButton.focus()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function closeSnackbar(id: number) {
|
|
36
|
+
const snackbarHasFocus = document.activeElement?.closest('[data-fds-snackbar]') != null
|
|
37
|
+
dismiss(id)
|
|
38
|
+
if (snackbarHasFocus) focusBeforeSnackbar?.focus()
|
|
39
|
+
focusBeforeSnackbar = null
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function handleAction(snackbar: FdsSnackbarItem) {
|
|
43
|
+
snackbar.action.onClick()
|
|
44
|
+
closeSnackbar(snackbar.id)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
onMounted(() => window.addEventListener('keydown', focusSnackbarAction))
|
|
48
|
+
onBeforeUnmount(() => window.removeEventListener('keydown', focusSnackbarAction))
|
|
49
|
+
</script>
|
|
50
|
+
|
|
51
|
+
<template>
|
|
52
|
+
<FdsToastRegions
|
|
53
|
+
v-if="isActiveContainer"
|
|
54
|
+
:items="snackbars"
|
|
55
|
+
:position="position"
|
|
56
|
+
:max="1"
|
|
57
|
+
widthClass="sm:w-xl"
|
|
58
|
+
>
|
|
59
|
+
<template #item="{ item }">
|
|
60
|
+
<FdsToastMessageCard
|
|
61
|
+
:message="item.message"
|
|
62
|
+
:type="item.type ?? 'info'"
|
|
63
|
+
:duration="item.duration ?? getReadingDuration(item.message, duration)"
|
|
64
|
+
:closeable="item.closeable ?? true"
|
|
65
|
+
:actionLabel="item.action.label"
|
|
66
|
+
:screenReaderHint="t('FdsSnackbar.focusHint')"
|
|
67
|
+
:dataTestid="item.dataTestid"
|
|
68
|
+
v-bind="{ 'data-fds-snackbar': '' }"
|
|
69
|
+
@action="handleAction(item)"
|
|
70
|
+
@close="closeSnackbar(item.id)"
|
|
71
|
+
/>
|
|
72
|
+
</template>
|
|
73
|
+
</FdsToastRegions>
|
|
74
|
+
</template>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { FdsToastMessageOptions, FdsToastMessagePosition } from '../FdsToastMessage/types'
|
|
2
|
+
|
|
3
|
+
export interface FdsSnackbarAction {
|
|
4
|
+
label: string
|
|
5
|
+
onClick: () => void
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface FdsSnackbarOptions extends Omit<FdsToastMessageOptions, 'heading'> {
|
|
9
|
+
action: FdsSnackbarAction
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface FdsSnackbarItem extends FdsSnackbarOptions {
|
|
13
|
+
id: number
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type FdsSnackbarShortcutOptions = Omit<FdsSnackbarOptions, 'message' | 'type'>
|
|
17
|
+
|
|
18
|
+
export interface FdsSnackbarProps {
|
|
19
|
+
position?: FdsToastMessagePosition
|
|
20
|
+
duration?: number
|
|
21
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createMessageQueue } from '../FdsToastMessage/createMessageQueue'
|
|
2
|
+
import type { FdsSnackbarOptions } from './types'
|
|
3
|
+
|
|
4
|
+
const snackbarQueue = createMessageQueue<FdsSnackbarOptions>()
|
|
5
|
+
|
|
6
|
+
export function useSnackbar() {
|
|
7
|
+
return {
|
|
8
|
+
snackbars: snackbarQueue.items,
|
|
9
|
+
show: snackbarQueue.show,
|
|
10
|
+
dismiss: snackbarQueue.dismiss,
|
|
11
|
+
clear: snackbarQueue.clear,
|
|
12
|
+
success: snackbarQueue.success,
|
|
13
|
+
info: snackbarQueue.info,
|
|
14
|
+
warning: snackbarQueue.warning,
|
|
15
|
+
error: snackbarQueue.error,
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const useSnackbarContainer = snackbarQueue.useContainer
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/vue3'
|
|
2
|
+
import FdsButtonSecondary from '../Buttons/FdsButtonSecondary/FdsButtonSecondary.vue'
|
|
3
|
+
import FdsToastMessage from './FdsToastMessage.vue'
|
|
4
|
+
import type { FdsToastMessagePosition, FdsToastMessageType } from './types'
|
|
5
|
+
import { useToastMessage } from './useToastMessage'
|
|
6
|
+
|
|
7
|
+
const meta: Meta<typeof FdsToastMessage> = {
|
|
8
|
+
title: 'FDS/FdsToastMessage',
|
|
9
|
+
component: FdsToastMessage,
|
|
10
|
+
tags: ['autodocs'],
|
|
11
|
+
argTypes: {
|
|
12
|
+
position: {
|
|
13
|
+
control: { type: 'select' },
|
|
14
|
+
options: ['top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right'],
|
|
15
|
+
},
|
|
16
|
+
duration: { control: { type: 'number' } },
|
|
17
|
+
max: { control: { type: 'number' } },
|
|
18
|
+
},
|
|
19
|
+
args: {
|
|
20
|
+
position: 'bottom-right',
|
|
21
|
+
duration: 5000,
|
|
22
|
+
max: 3,
|
|
23
|
+
},
|
|
24
|
+
parameters: {
|
|
25
|
+
docs: {
|
|
26
|
+
description: {
|
|
27
|
+
component:
|
|
28
|
+
'Place `<FdsToastMessage />` once in App.vue and show messages from anywhere with `useToastMessage()`. Toasts pause while hovered or focused, can be swiped away on touch screens and wait in a queue when more than `max` are shown at the same position. `duration` is the shortest time a success or info toast is shown; longer texts stay 1 s plus 300 ms per word. Error and warning toasts stay until they are closed, unless `duration` is set on the toast.',
|
|
29
|
+
},
|
|
30
|
+
source: {
|
|
31
|
+
code: `<!-- App.vue -->
|
|
32
|
+
<FdsToastMessage position="bottom-right" :duration="5000" :max="3" />
|
|
33
|
+
|
|
34
|
+
// Anywhere
|
|
35
|
+
const toast = useToastMessage()
|
|
36
|
+
toast.success('Booking saved')
|
|
37
|
+
toast.error('Something went wrong', { heading: 'Error' })
|
|
38
|
+
toast.show({ type: 'info', message: 'New version available', position: 'bottom-center' })`,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default meta
|
|
45
|
+
type Story = StoryObj<typeof meta>
|
|
46
|
+
|
|
47
|
+
const messages: Record<FdsToastMessageType, { heading: string; message: string }> = {
|
|
48
|
+
success: { heading: 'Saved', message: 'The booking has been saved.' },
|
|
49
|
+
info: { heading: 'Information', message: 'A new version of the app is available.' },
|
|
50
|
+
warning: { heading: 'Warning', message: 'Your session ends in 5 minutes.' },
|
|
51
|
+
error: { heading: 'Something went wrong', message: 'The booking could not be saved. Try again.' },
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const Default: Story = {
|
|
55
|
+
render: (args) => ({
|
|
56
|
+
components: { FdsToastMessage, FdsButtonSecondary },
|
|
57
|
+
setup() {
|
|
58
|
+
const toast = useToastMessage()
|
|
59
|
+
const types: FdsToastMessageType[] = ['success', 'info', 'warning', 'error']
|
|
60
|
+
const showToast = (type: FdsToastMessageType) => toast.show({ type, ...messages[type] })
|
|
61
|
+
return { args, types, showToast, clear: toast.clear }
|
|
62
|
+
},
|
|
63
|
+
template: `
|
|
64
|
+
<div class="flex flex-wrap gap-2">
|
|
65
|
+
<FdsButtonSecondary v-for="type in types" :key="type" :text="type" @click="showToast(type)" />
|
|
66
|
+
<FdsButtonSecondary text="Clear all" @click="clear" />
|
|
67
|
+
</div>
|
|
68
|
+
<FdsToastMessage v-bind="args" />
|
|
69
|
+
`,
|
|
70
|
+
}),
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const Positions: Story = {
|
|
74
|
+
render: (args) => ({
|
|
75
|
+
components: { FdsToastMessage, FdsButtonSecondary },
|
|
76
|
+
setup() {
|
|
77
|
+
const toast = useToastMessage()
|
|
78
|
+
const positions: FdsToastMessagePosition[] = [
|
|
79
|
+
'top-left',
|
|
80
|
+
'top-center',
|
|
81
|
+
'top-right',
|
|
82
|
+
'bottom-left',
|
|
83
|
+
'bottom-center',
|
|
84
|
+
'bottom-right',
|
|
85
|
+
]
|
|
86
|
+
const showToast = (position: FdsToastMessagePosition) => toast.info(`Shown at ${position}`, { position })
|
|
87
|
+
return { args, positions, showToast }
|
|
88
|
+
},
|
|
89
|
+
template: `
|
|
90
|
+
<div class="grid grid-cols-3 gap-2 max-w-xl">
|
|
91
|
+
<FdsButtonSecondary v-for="position in positions" :key="position" :text="position" @click="showToast(position)" />
|
|
92
|
+
</div>
|
|
93
|
+
<FdsToastMessage v-bind="args" />
|
|
94
|
+
`,
|
|
95
|
+
}),
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const QueueAndPersistent: Story = {
|
|
99
|
+
render: (args) => ({
|
|
100
|
+
components: { FdsToastMessage, FdsButtonSecondary },
|
|
101
|
+
setup() {
|
|
102
|
+
const toast = useToastMessage()
|
|
103
|
+
let count = 0
|
|
104
|
+
const showMany = () => {
|
|
105
|
+
for (let index = 0; index < 6; index += 1) {
|
|
106
|
+
count += 1
|
|
107
|
+
toast.success(`Message ${count}`, { duration: 3000 })
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const showPersistent = () =>
|
|
111
|
+
toast.error('This message stays until it is closed or swiped away.', { heading: 'Error' })
|
|
112
|
+
return { args, showMany, showPersistent }
|
|
113
|
+
},
|
|
114
|
+
template: `
|
|
115
|
+
<div class="flex flex-wrap gap-2">
|
|
116
|
+
<FdsButtonSecondary text="Show 6 (max 3 at a time)" @click="showMany" />
|
|
117
|
+
<FdsButtonSecondary text="Show persistent error" @click="showPersistent" />
|
|
118
|
+
</div>
|
|
119
|
+
<FdsToastMessage v-bind="args" />
|
|
120
|
+
`,
|
|
121
|
+
}),
|
|
122
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import FdsToastMessageCard from './FdsToastMessageCard.vue'
|
|
3
|
+
import FdsToastRegions from './FdsToastRegions.vue'
|
|
4
|
+
import { getReadingDuration } from './getReadingDuration'
|
|
5
|
+
import type { FdsToastMessageItem, FdsToastMessageProps } from './types'
|
|
6
|
+
import { useToastMessage, useToastMessageContainer } from './useToastMessage'
|
|
7
|
+
|
|
8
|
+
withDefaults(defineProps<FdsToastMessageProps>(), {
|
|
9
|
+
position: 'bottom-right',
|
|
10
|
+
duration: 5000,
|
|
11
|
+
max: 3,
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
const { toasts, dismiss } = useToastMessage()
|
|
15
|
+
const isActiveContainer = useToastMessageContainer()
|
|
16
|
+
|
|
17
|
+
function getDuration(toast: FdsToastMessageItem, minimumDuration: number) {
|
|
18
|
+
if (toast.duration !== undefined) return toast.duration
|
|
19
|
+
if (toast.type === 'error' || toast.type === 'warning') return 0
|
|
20
|
+
return getReadingDuration(`${toast.heading ?? ''} ${toast.message}`, minimumDuration)
|
|
21
|
+
}
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<template>
|
|
25
|
+
<FdsToastRegions
|
|
26
|
+
v-if="isActiveContainer"
|
|
27
|
+
:items="toasts"
|
|
28
|
+
:position="position"
|
|
29
|
+
:max="max"
|
|
30
|
+
widthClass="sm:w-96"
|
|
31
|
+
>
|
|
32
|
+
<template #item="{ item }">
|
|
33
|
+
<FdsToastMessageCard
|
|
34
|
+
:message="item.message"
|
|
35
|
+
:heading="item.heading"
|
|
36
|
+
:type="item.type ?? 'info'"
|
|
37
|
+
:duration="getDuration(item, duration)"
|
|
38
|
+
:closeable="item.closeable ?? true"
|
|
39
|
+
:dataTestid="item.dataTestid"
|
|
40
|
+
@close="dismiss(item.id)"
|
|
41
|
+
/>
|
|
42
|
+
</template>
|
|
43
|
+
</FdsToastRegions>
|
|
44
|
+
</template>
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
3
|
+
import { useFdsI18n } from '../../plugin/useFdsI18n'
|
|
4
|
+
import FdsButtonIcon from '../Buttons/FdsButtonIcon/FdsButtonIcon.vue'
|
|
5
|
+
import FdsButtonMinor from '../Buttons/FdsButtonMinor/FdsButtonMinor.vue'
|
|
6
|
+
import FdsIcon from '../FdsIcon/FdsIcon.vue'
|
|
7
|
+
import type { FdsIconName } from '../FdsIcon/types'
|
|
8
|
+
import type { FdsToastMessageType } from './types'
|
|
9
|
+
|
|
10
|
+
const props = defineProps<{
|
|
11
|
+
message: string
|
|
12
|
+
heading?: string
|
|
13
|
+
type: FdsToastMessageType
|
|
14
|
+
duration: number
|
|
15
|
+
closeable: boolean
|
|
16
|
+
actionLabel?: string
|
|
17
|
+
screenReaderHint?: string
|
|
18
|
+
dataTestid?: string
|
|
19
|
+
}>()
|
|
20
|
+
|
|
21
|
+
const emit = defineEmits<{
|
|
22
|
+
(e: 'close'): void
|
|
23
|
+
(e: 'action'): void
|
|
24
|
+
}>()
|
|
25
|
+
|
|
26
|
+
const { t } = useFdsI18n()
|
|
27
|
+
|
|
28
|
+
const typeStyles: Record<FdsToastMessageType, { card: string; icon: string; action: string; iconName: FdsIconName }> =
|
|
29
|
+
{
|
|
30
|
+
success: { card: 'bg-green-100 inset-ring-green-300', icon: 'fill-green-700', action: '', iconName: 'success' },
|
|
31
|
+
info: { card: 'bg-blue-100 inset-ring-blue-300', icon: 'fill-blue-700', action: '', iconName: 'information' },
|
|
32
|
+
warning: {
|
|
33
|
+
card: 'bg-yellow-200 inset-ring-yellow-300',
|
|
34
|
+
icon: 'fill-yellow-700',
|
|
35
|
+
action:
|
|
36
|
+
'[&_button]:text-gray-900! [&_button]:underline [&_button]:hover:bg-gray-100! [&_button]:active:bg-gray-200!',
|
|
37
|
+
iconName: 'warning',
|
|
38
|
+
},
|
|
39
|
+
error: { card: 'bg-red-100 inset-ring-red-300', icon: 'fill-red-700', action: '', iconName: 'alert' },
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const style = computed(() => typeStyles[props.type])
|
|
43
|
+
|
|
44
|
+
const typeLabel = computed(() => {
|
|
45
|
+
const labels: Record<FdsToastMessageType, string> = {
|
|
46
|
+
success: t('FdsToastMessage.type.success'),
|
|
47
|
+
info: t('FdsToastMessage.type.info'),
|
|
48
|
+
warning: t('FdsToastMessage.type.warning'),
|
|
49
|
+
error: t('FdsToastMessage.type.error'),
|
|
50
|
+
}
|
|
51
|
+
return labels[props.type]
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const isHovered = ref(false)
|
|
55
|
+
const isFocused = ref(false)
|
|
56
|
+
const isSwiping = ref(false)
|
|
57
|
+
const swipeOffset = ref(0)
|
|
58
|
+
let swipeStartX = 0
|
|
59
|
+
let swipeStartTime = 0
|
|
60
|
+
|
|
61
|
+
const isPaused = computed(() => isHovered.value || isFocused.value || isSwiping.value)
|
|
62
|
+
|
|
63
|
+
const cardStyle = computed(() => ({
|
|
64
|
+
transform: swipeOffset.value ? `translateX(${swipeOffset.value}px)` : undefined,
|
|
65
|
+
opacity: swipeOffset.value ? Math.max(0.3, 1 - Math.abs(swipeOffset.value) / 250) : undefined,
|
|
66
|
+
}))
|
|
67
|
+
|
|
68
|
+
let remainingTime = props.duration
|
|
69
|
+
let closeTimer: number | undefined
|
|
70
|
+
let timerStartedAt = 0
|
|
71
|
+
|
|
72
|
+
function startCloseTimer() {
|
|
73
|
+
if (props.duration <= 0 || closeTimer !== undefined) return
|
|
74
|
+
timerStartedAt = Date.now()
|
|
75
|
+
closeTimer = window.setTimeout(() => emit('close'), remainingTime)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function pauseCloseTimer() {
|
|
79
|
+
if (closeTimer === undefined) return
|
|
80
|
+
window.clearTimeout(closeTimer)
|
|
81
|
+
closeTimer = undefined
|
|
82
|
+
remainingTime -= Date.now() - timerStartedAt
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
watch(isPaused, (paused) => (paused ? pauseCloseTimer() : startCloseTimer()))
|
|
86
|
+
|
|
87
|
+
onMounted(startCloseTimer)
|
|
88
|
+
|
|
89
|
+
onBeforeUnmount(pauseCloseTimer)
|
|
90
|
+
|
|
91
|
+
function handlePointerDown(event: PointerEvent) {
|
|
92
|
+
if (event.pointerType === 'mouse' || (event.target instanceof Element && event.target.closest('button, a'))) return
|
|
93
|
+
isSwiping.value = true
|
|
94
|
+
swipeStartX = event.clientX
|
|
95
|
+
swipeStartTime = event.timeStamp
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function handlePointerMove(event: PointerEvent) {
|
|
99
|
+
if (!isSwiping.value) return
|
|
100
|
+
const distance = event.clientX - swipeStartX
|
|
101
|
+
if (swipeOffset.value === 0 && Math.abs(distance) < 10) return
|
|
102
|
+
swipeOffset.value = distance
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function handlePointerUp(event: PointerEvent) {
|
|
106
|
+
if (!isSwiping.value) return
|
|
107
|
+
isSwiping.value = false
|
|
108
|
+
const distance = Math.abs(swipeOffset.value)
|
|
109
|
+
const speed = distance / Math.max(1, event.timeStamp - swipeStartTime)
|
|
110
|
+
if (distance > 100 || (distance > 30 && speed > 0.5)) {
|
|
111
|
+
emit('close')
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
swipeOffset.value = 0
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function handlePointerCancel() {
|
|
118
|
+
isSwiping.value = false
|
|
119
|
+
swipeOffset.value = 0
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function handleFocusOut(event: FocusEvent) {
|
|
123
|
+
if (event.currentTarget instanceof Element && event.relatedTarget instanceof Node) {
|
|
124
|
+
isFocused.value = event.currentTarget.contains(event.relatedTarget)
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
isFocused.value = false
|
|
128
|
+
}
|
|
129
|
+
</script>
|
|
130
|
+
|
|
131
|
+
<template>
|
|
132
|
+
<div
|
|
133
|
+
:role="type === 'error' ? 'alert' : undefined"
|
|
134
|
+
v-bind="{ 'data-testid': dataTestid }"
|
|
135
|
+
:class="[
|
|
136
|
+
'pointer-events-auto flex items-start gap-3 overflow-hidden rounded-md inset-ring-1 p-4 shadow-lg touch-pan-y',
|
|
137
|
+
style.card,
|
|
138
|
+
isSwiping ? '' : 'transition-[transform,opacity] duration-200 ease-out',
|
|
139
|
+
swipeOffset !== 0 && 'select-none',
|
|
140
|
+
]"
|
|
141
|
+
:style="cardStyle"
|
|
142
|
+
@mouseenter="isHovered = true"
|
|
143
|
+
@mouseleave="isHovered = false"
|
|
144
|
+
@focusin="isFocused = true"
|
|
145
|
+
@focusout="handleFocusOut"
|
|
146
|
+
@keydown.esc="closeable && emit('close')"
|
|
147
|
+
@pointerdown="handlePointerDown"
|
|
148
|
+
@pointermove="handlePointerMove"
|
|
149
|
+
@pointerup="handlePointerUp"
|
|
150
|
+
@pointercancel="handlePointerCancel"
|
|
151
|
+
>
|
|
152
|
+
<div class="h-7 flex items-center shrink-0">
|
|
153
|
+
<FdsIcon :name="style.iconName" :size="24" :class="style.icon" aria-hidden="true" />
|
|
154
|
+
</div>
|
|
155
|
+
<div class="flex-1 min-w-0 flex flex-col py-0.5 break-words">
|
|
156
|
+
<span class="sr-only">{{ typeLabel }}</span>
|
|
157
|
+
<span v-if="heading" class="font-bold">{{ heading }}</span>
|
|
158
|
+
<span>{{ message }}</span>
|
|
159
|
+
<span v-if="screenReaderHint" class="sr-only">{{ screenReaderHint }}</span>
|
|
160
|
+
</div>
|
|
161
|
+
<div v-if="actionLabel" class="flex flex-col justify-center self-center shrink-0">
|
|
162
|
+
<FdsButtonMinor :text="actionLabel" :class="style.action" @click="emit('action')" />
|
|
163
|
+
</div>
|
|
164
|
+
<FdsButtonIcon
|
|
165
|
+
v-if="closeable"
|
|
166
|
+
icon="cross"
|
|
167
|
+
:size="28"
|
|
168
|
+
class="shrink-0 fill-gray-900! hover:bg-gray-100 active:bg-gray-200"
|
|
169
|
+
v-bind="{ 'aria-label': t('FdsToastMessage.close') }"
|
|
170
|
+
@click="emit('close')"
|
|
171
|
+
/>
|
|
172
|
+
</div>
|
|
173
|
+
</template>
|