fds-vue-core 9.0.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.
Files changed (75) hide show
  1. package/components.d.ts +4 -0
  2. package/dist/components/Buttons/FdsButtonIcon/FdsButtonIcon.vue_vue_type_script_setup_true_lang.js +1 -0
  3. package/dist/components/Buttons/FdsButtonIcon/FdsButtonIcon.vue_vue_type_script_setup_true_lang.js.map +1 -1
  4. package/dist/components/Buttons/FdsButtonIcon/types.d.ts +1 -0
  5. package/dist/components/FdsSearchSelectPro/FdsSearchSelectPro.js.map +1 -1
  6. package/dist/components/FdsSearchSelectPro/FdsSearchSelectPro.vue.d.ts +1 -0
  7. package/dist/components/FdsSearchSelectPro/FdsSearchSelectPro.vue_vue_type_script_setup_true_lang.js +4 -0
  8. package/dist/components/FdsSearchSelectPro/FdsSearchSelectPro.vue_vue_type_script_setup_true_lang.js.map +1 -1
  9. package/dist/components/FdsSearchSelectPro/types.d.ts +2 -0
  10. package/dist/components/FdsSearchSelectPro/useSearchSelectProItems.js +1 -1
  11. package/dist/components/FdsSearchSelectPro/useSearchSelectProItems.js.map +1 -1
  12. package/dist/components/FdsSnackbar/FdsSnackbar.js +6 -0
  13. package/dist/components/FdsSnackbar/FdsSnackbar.js.map +1 -0
  14. package/dist/components/FdsSnackbar/FdsSnackbar.vue.d.ts +7 -0
  15. package/dist/components/FdsSnackbar/FdsSnackbar.vue_vue_type_script_setup_true_lang.js +78 -0
  16. package/dist/components/FdsSnackbar/FdsSnackbar.vue_vue_type_script_setup_true_lang.js.map +1 -0
  17. package/dist/components/FdsSnackbar/types.d.ts +16 -0
  18. package/dist/components/FdsSnackbar/useSnackbar.d.ts +40 -0
  19. package/dist/components/FdsSnackbar/useSnackbar.js +19 -0
  20. package/dist/components/FdsSnackbar/useSnackbar.js.map +1 -0
  21. package/dist/components/FdsToastMessage/FdsToastMessage.js +6 -0
  22. package/dist/components/FdsToastMessage/FdsToastMessage.js.map +1 -0
  23. package/dist/components/FdsToastMessage/FdsToastMessage.vue.d.ts +8 -0
  24. package/dist/components/FdsToastMessage/FdsToastMessage.vue_vue_type_script_setup_true_lang.js +58 -0
  25. package/dist/components/FdsToastMessage/FdsToastMessage.vue_vue_type_script_setup_true_lang.js.map +1 -0
  26. package/dist/components/FdsToastMessage/FdsToastMessageCard.js +6 -0
  27. package/dist/components/FdsToastMessage/FdsToastMessageCard.js.map +1 -0
  28. package/dist/components/FdsToastMessage/FdsToastMessageCard.vue.d.ts +20 -0
  29. package/dist/components/FdsToastMessage/FdsToastMessageCard.vue_vue_type_script_setup_true_lang.js +185 -0
  30. package/dist/components/FdsToastMessage/FdsToastMessageCard.vue_vue_type_script_setup_true_lang.js.map +1 -0
  31. package/dist/components/FdsToastMessage/FdsToastRegions.js +8 -0
  32. package/dist/components/FdsToastMessage/FdsToastRegions.js.map +1 -0
  33. package/dist/components/FdsToastMessage/FdsToastRegions.vue.d.ts +28 -0
  34. package/dist/components/FdsToastMessage/FdsToastRegions.vue_vue_type_script_setup_true_lang.js +81 -0
  35. package/dist/components/FdsToastMessage/FdsToastRegions.vue_vue_type_script_setup_true_lang.js.map +1 -0
  36. package/dist/components/FdsToastMessage/createMessageQueue.d.ts +19 -0
  37. package/dist/components/FdsToastMessage/createMessageQueue.js +50 -0
  38. package/dist/components/FdsToastMessage/createMessageQueue.js.map +1 -0
  39. package/dist/components/FdsToastMessage/getReadingDuration.d.ts +1 -0
  40. package/dist/components/FdsToastMessage/getReadingDuration.js +11 -0
  41. package/dist/components/FdsToastMessage/getReadingDuration.js.map +1 -0
  42. package/dist/components/FdsToastMessage/types.d.ts +20 -0
  43. package/dist/components/FdsToastMessage/useToastMessage.d.ts +40 -0
  44. package/dist/components/FdsToastMessage/useToastMessage.js +19 -0
  45. package/dist/components/FdsToastMessage/useToastMessage.js.map +1 -0
  46. package/dist/fds-vue-core.css +2 -2
  47. package/dist/index.d.ts +8 -2
  48. package/dist/index.js +9 -3
  49. package/dist/index.js.map +1 -1
  50. package/dist/lang/en.js +6 -0
  51. package/dist/lang/en.js.map +1 -1
  52. package/dist/lang/sv.js +6 -0
  53. package/dist/lang/sv.js.map +1 -1
  54. package/dist/tsconfig.build.tsbuildinfo +1 -1
  55. package/package.json +13 -13
  56. package/src/components/Buttons/FdsButtonIcon/types.ts +1 -0
  57. package/src/components/FdsSearchSelectPro/FdsSearchSelectPro.stories.ts +24 -0
  58. package/src/components/FdsSearchSelectPro/FdsSearchSelectPro.vue +1 -0
  59. package/src/components/FdsSearchSelectPro/types.ts +2 -0
  60. package/src/components/FdsSearchSelectPro/useSearchSelectProItems.ts +1 -1
  61. package/src/components/FdsSnackbar/FdsSnackbar.stories.ts +111 -0
  62. package/src/components/FdsSnackbar/FdsSnackbar.vue +74 -0
  63. package/src/components/FdsSnackbar/types.ts +21 -0
  64. package/src/components/FdsSnackbar/useSnackbar.ts +19 -0
  65. package/src/components/FdsToastMessage/FdsToastMessage.stories.ts +122 -0
  66. package/src/components/FdsToastMessage/FdsToastMessage.vue +44 -0
  67. package/src/components/FdsToastMessage/FdsToastMessageCard.vue +173 -0
  68. package/src/components/FdsToastMessage/FdsToastRegions.vue +114 -0
  69. package/src/components/FdsToastMessage/createMessageQueue.ts +57 -0
  70. package/src/components/FdsToastMessage/getReadingDuration.ts +8 -0
  71. package/src/components/FdsToastMessage/types.ts +31 -0
  72. package/src/components/FdsToastMessage/useToastMessage.ts +19 -0
  73. package/src/index.ts +27 -0
  74. package/src/lang/en.json +6 -0
  75. 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.0.0",
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.0",
49
+ "axios": "1.21.1",
50
50
  "date-fns": "4.4.0",
51
51
  "imask": "7.6.1",
52
- "libphonenumber-js": "1.13.15",
52
+ "libphonenumber-js": "1.13.16",
53
53
  "personnummer": "3.3.1",
54
54
  "qrcode.vue": "3.11.0",
55
- "tailwindcss": "4.3.4"
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.5",
61
- "@storybook/addon-docs": "10.6.5",
62
- "@storybook/addon-vitest": "10.6.5",
63
- "@storybook/vue3": "10.6.5",
64
- "@storybook/vue3-vite": "10.6.5",
65
- "@tailwindcss/vite": "4.3.4",
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.0",
71
+ "fg-devkit": "1.29.1",
72
72
  "happy-dom": "21.0.4",
73
- "playwright": "1.64.1",
74
- "storybook": "10.6.5",
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",
@@ -5,5 +5,6 @@ export interface FdsIconButtonProps extends /* @vue-ignore */ ButtonHTMLAttribut
5
5
  icon: FdsIconName
6
6
  size?: number
7
7
  loading?: boolean
8
+ type?: 'button' | 'submit' | 'reset'
8
9
  dataTestid?: string
9
10
  }
@@ -39,6 +39,10 @@ const meta: Meta<typeof FdsSearchSelect> = {
39
39
  control: { type: 'boolean' },
40
40
  description: 'Allows divider rows to be selectable',
41
41
  },
42
+ keepDividerOrder: {
43
+ control: { type: 'boolean' },
44
+ description: 'Keeps explicit divider groups in the given order instead of sorting them by label',
45
+ },
42
46
  showLoadMore: {
43
47
  control: { type: 'boolean' },
44
48
  description: 'Shows load-more action row at the end of list',
@@ -684,6 +688,26 @@ export const SingleSelectWithSelectableDivider: Story = {
684
688
  },
685
689
  }
686
690
 
691
+ export const KeepDividerOrder: Story = {
692
+ args: {
693
+ items: [
694
+ { id: 'shown', name: 'Visas i App och Portal', isDivider: true },
695
+ { id: 1, name: 'Hälsoundersökning' },
696
+ { id: 2, name: 'Vaccination' },
697
+ { id: 'hidden', name: 'Visas ej i App och Portal', isDivider: true },
698
+ { id: 3, name: 'Hörseltest' },
699
+ { id: 'scheduling', name: 'Schemaläggning: Visas som fysiskt besök', isDivider: true },
700
+ { id: 4, name: 'Syntest' },
701
+ ],
702
+ keepDividerOrder: true,
703
+ itemKey: 'id',
704
+ searchFields: ['name'],
705
+ label: 'Tjänstetyp',
706
+ searchContext: { context: 'tjänstetyper', linkWord: 'av' },
707
+ noResultPrompt: 'Inga resultat hittades',
708
+ },
709
+ }
710
+
687
711
  export const MultipleWithLoadMore: Story = {
688
712
  args: {
689
713
  items: largeHierarchicalItems,
@@ -34,6 +34,7 @@ const props = withDefaults(defineProps<FdsSearchSelectProProps>(), {
34
34
  itemKey: undefined,
35
35
  dividerField: 'isDivider',
36
36
  dividerSelectable: false,
37
+ keepDividerOrder: false,
37
38
  autoDividerBy: undefined,
38
39
  levelField: 'level',
39
40
  showSelectedToggle: false,
@@ -29,6 +29,8 @@ export interface FdsSearchSelectProProps extends Omit</* @vue-ignore */ HTMLAttr
29
29
  dividerField?: string
30
30
  /** Allows divider rows/group headers to be selectable as regular options. */
31
31
  dividerSelectable?: boolean
32
+ /** Keeps explicit divider groups in the order given in `items` instead of sorting them by label. */
33
+ keepDividerOrder?: boolean
32
34
  /** Field name used to auto-insert divider groups from raw items (for example region). */
33
35
  autoDividerBy?: string
34
36
  /** Field name containing hierarchy level number (defaults to `level`). */
@@ -190,7 +190,7 @@ export const useSearchSelectProItems = ({
190
190
  if (!groupField) {
191
191
  const dividerField = props.dividerField ?? 'isDivider'
192
192
  const hasExplicitDividers = baseItems.some((item) => item[dividerField] === true || item.type === 'divider')
193
- if (!hasExplicitDividers) return baseItems
193
+ if (!hasExplicitDividers || props.keepDividerOrder) return baseItems
194
194
 
195
195
  const locale = resolvedLocale.value
196
196
  return reorderExplicitDividerGroups(baseItems, locale)
@@ -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>