@thekestongroup/ui 0.15.0 → 0.18.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/ActionList/ActionList.vue.d.ts +74 -0
- package/dist/components/Alert/Alert.vue.d.ts +55 -0
- package/dist/components/AsyncState/AsyncState.vue.d.ts +53 -0
- package/dist/components/AvatarGroup/AvatarGroup.vue.d.ts +1 -1
- package/dist/components/BarChart/BarChart.vue.d.ts +37 -0
- package/dist/components/BarChart/ChartData.vue.d.ts +20 -0
- package/dist/components/Button/Button.vue.d.ts +1 -1
- package/dist/components/Collapsible/Collapsible.vue.d.ts +48 -0
- package/dist/components/ConfirmDialog/ConfirmDialog.vue.d.ts +62 -0
- package/dist/components/FileUpload/FileUpload.vue.d.ts +55 -0
- package/dist/components/Footer/Footer.vue.d.ts +11 -1
- package/dist/components/Header/Header.vue.d.ts +1 -1
- package/dist/components/InputRichText/InputRichText.vue.d.ts +12 -6
- package/dist/components/LineChart/LineChart.vue.d.ts +37 -0
- package/dist/components/Notification/Notification.vue.d.ts +2 -2
- package/dist/components/Progress/Progress.vue.d.ts +14 -1
- package/dist/components/Prose/Prose.vue.d.ts +36 -0
- package/dist/components/ResultState/ResultState.vue.d.ts +48 -0
- package/dist/components/Slideover/Slideover.vue.d.ts +7 -5
- package/dist/components/Spinner/Spinner.vue.d.ts +1 -1
- package/dist/components/Steps/Steps.vue.d.ts +16 -5
- package/dist/components/ThemeSwitcher/ThemeSwitcher.vue.d.ts +18 -0
- package/dist/i18n/en.d.ts +43 -0
- package/dist/index.d.ts +25 -0
- package/dist/keston-ui.js +5287 -4028
- package/dist/lib/chart.d.ts +32 -0
- package/dist/lib/initials.d.ts +6 -1
- package/dist/lib/theme.d.ts +23 -0
- package/package.json +10 -3
- package/src/components/ActionList/ActionList.stories.ts +92 -0
- package/src/components/ActionList/ActionList.vue +138 -0
- package/src/components/Alert/Alert.stories.ts +65 -0
- package/src/components/Alert/Alert.vue +110 -0
- package/src/components/AsyncState/AsyncState.stories.ts +46 -0
- package/src/components/AsyncState/AsyncState.vue +78 -0
- package/src/components/BarChart/BarChart.stories.ts +62 -0
- package/src/components/BarChart/BarChart.vue +153 -0
- package/src/components/BarChart/ChartData.vue +64 -0
- package/src/components/Button/Button.vue +4 -4
- package/src/components/Calendar/Calendar.vue +3 -2
- package/src/components/CardSelect/CardSelect.vue +1 -1
- package/src/components/Checkbox/Checkbox.vue +1 -1
- package/src/components/CheckboxGroup/CheckboxGroup.vue +1 -1
- package/src/components/Collapsible/Collapsible.stories.ts +32 -0
- package/src/components/Collapsible/Collapsible.vue +68 -0
- package/src/components/ConfirmDialog/ConfirmDialog.stories.ts +54 -0
- package/src/components/ConfirmDialog/ConfirmDialog.vue +119 -0
- package/src/components/FileUpload/FileUpload.stories.ts +38 -0
- package/src/components/FileUpload/FileUpload.vue +150 -0
- package/src/components/Footer/Footer.stories.ts +10 -2
- package/src/components/Footer/Footer.vue +22 -4
- package/src/components/Header/Header.stories.ts +20 -0
- package/src/components/InputRichText/InputRichText.stories.ts +19 -0
- package/src/components/InputRichText/InputRichText.vue +129 -13
- package/src/components/LineChart/LineChart.stories.ts +47 -0
- package/src/components/LineChart/LineChart.vue +173 -0
- package/src/components/Notification/Toaster.vue +3 -1
- package/src/components/Progress/Progress.stories.ts +18 -0
- package/src/components/Progress/Progress.vue +57 -0
- package/src/components/Prose/Prose.stories.ts +46 -0
- package/src/components/Prose/Prose.vue +54 -0
- package/src/components/RadioGroup/RadioGroup.vue +2 -2
- package/src/components/ResultState/ResultState.stories.ts +44 -0
- package/src/components/ResultState/ResultState.vue +76 -0
- package/src/components/Select/Select.vue +6 -4
- package/src/components/Slideover/Slideover.vue +107 -31
- package/src/components/Steps/Steps.stories.ts +18 -0
- package/src/components/Steps/Steps.vue +28 -8
- package/src/components/ThemeSwitcher/ThemeSwitcher.stories.ts +22 -0
- package/src/components/ThemeSwitcher/ThemeSwitcher.vue +48 -0
- package/src/components/Tooltip/Tooltip.vue +1 -1
- package/src/i18n/en.ts +29 -7
- package/src/index.ts +25 -0
- package/src/lib/chart.ts +47 -0
- package/src/lib/initials.test.ts +19 -0
- package/src/lib/initials.ts +12 -3
- package/src/lib/theme.ts +75 -0
- package/src/styles/theme.css +34 -2
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/vue3-vite'
|
|
2
|
+
import { ref } from 'vue'
|
|
3
|
+
import FileUpload from './FileUpload.vue'
|
|
4
|
+
|
|
5
|
+
const meta = {
|
|
6
|
+
title: 'Components/Form/FileUpload',
|
|
7
|
+
component: FileUpload,
|
|
8
|
+
tags: ['autodocs'],
|
|
9
|
+
args: { drop: false, multiple: false, disabled: false, accept: undefined },
|
|
10
|
+
} satisfies Meta<typeof FileUpload>
|
|
11
|
+
|
|
12
|
+
export default meta
|
|
13
|
+
type Story = StoryObj<typeof meta>
|
|
14
|
+
|
|
15
|
+
/** Just the button, e.g. "Upload a photo" beside an avatar. */
|
|
16
|
+
export const Default: Story = {}
|
|
17
|
+
|
|
18
|
+
/** With a drop area: drag files onto it, or press the button. */
|
|
19
|
+
export const Drop: Story = {
|
|
20
|
+
args: { drop: true },
|
|
21
|
+
render: (args) => ({
|
|
22
|
+
components: { FileUpload },
|
|
23
|
+
setup: () => ({ args, names: ref<string[]>([]) }),
|
|
24
|
+
template: `
|
|
25
|
+
<div class="max-w-xl">
|
|
26
|
+
<FileUpload v-bind="args" @select="names = $event.map(f => f.name)">
|
|
27
|
+
<p v-if="names.length" class="text-sm font-bold">{{ names.join(', ') }}</p>
|
|
28
|
+
</FileUpload>
|
|
29
|
+
</div>`,
|
|
30
|
+
}),
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Several files, of one type. Dropped files of other types are ignored, as in the picker. */
|
|
34
|
+
export const MultiplePdfs: Story = { args: { drop: true, multiple: true, accept: 'application/pdf,.pdf' } }
|
|
35
|
+
|
|
36
|
+
export const CustomLabel: Story = { args: { label: 'Import a CSV', accept: '.csv,text/csv' } }
|
|
37
|
+
|
|
38
|
+
export const Disabled: Story = { args: { drop: true, disabled: true } }
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, ref, type HTMLAttributes } from 'vue'
|
|
3
|
+
import { useI18n } from 'vue-i18n'
|
|
4
|
+
import { FileUp, Upload } from '@lucide/vue'
|
|
5
|
+
import { cn } from '../../lib/cn'
|
|
6
|
+
import Button, { type ButtonProps } from '../Button/Button.vue'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Choosing files to upload: a Button that opens the device's file picker
|
|
10
|
+
* and, with `drop`, a dashed area around it that files can be dragged onto.
|
|
11
|
+
* Emits `select` with the chosen files (only ones `accept` allows; just the
|
|
12
|
+
* first unless `multiple`). It doesn't upload anything itself.
|
|
13
|
+
*
|
|
14
|
+
* The button is always there, so it works by keyboard, on touch screens and
|
|
15
|
+
* with screen readers; dropping is a shortcut for mouse users.
|
|
16
|
+
*
|
|
17
|
+
* Slots: default (in the drop area above the button, e.g. the chosen file's
|
|
18
|
+
* name; replaces the description), leading (the button's icon).
|
|
19
|
+
*/
|
|
20
|
+
export interface FileUploadProps {
|
|
21
|
+
/** The button's text. Default "Choose a file" (or "Choose files" with `multiple`). */
|
|
22
|
+
label?: string
|
|
23
|
+
/** File types to allow, as for `<input accept>`: ".csv,text/csv", "image/*". */
|
|
24
|
+
accept?: string
|
|
25
|
+
multiple?: boolean
|
|
26
|
+
/** Show a drop area around the button. */
|
|
27
|
+
drop?: boolean
|
|
28
|
+
/** Text in the drop area. Default "Drop a file here, or choose one." */
|
|
29
|
+
description?: string
|
|
30
|
+
disabled?: boolean
|
|
31
|
+
/** The button's look; default basic. */
|
|
32
|
+
variant?: ButtonProps['variant']
|
|
33
|
+
size?: ButtonProps['size']
|
|
34
|
+
/** Button status, e.g. `loading` while the chosen file uploads. */
|
|
35
|
+
status?: ButtonProps['status']
|
|
36
|
+
/** Text beside the spinner while `status` is loading, e.g. "Uploading…". */
|
|
37
|
+
loadingText?: string
|
|
38
|
+
class?: HTMLAttributes['class']
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const { t } = useI18n()
|
|
42
|
+
const props = withDefaults(defineProps<FileUploadProps>(), {
|
|
43
|
+
variant: 'basic',
|
|
44
|
+
size: 'md',
|
|
45
|
+
status: 'idle'
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const emit = defineEmits<{
|
|
49
|
+
/** Files were chosen or dropped. */
|
|
50
|
+
select: [files: File[]]
|
|
51
|
+
}>()
|
|
52
|
+
|
|
53
|
+
defineSlots<{
|
|
54
|
+
default?(): unknown
|
|
55
|
+
leading?(): unknown
|
|
56
|
+
}>()
|
|
57
|
+
|
|
58
|
+
const input = ref<HTMLInputElement>()
|
|
59
|
+
|
|
60
|
+
const buttonLabel = computed(() => props.label ?? (props.multiple ? t('ui.fileUpload.chooseFiles') : t('ui.fileUpload.chooseFile')))
|
|
61
|
+
const dropText = computed(() => props.description ?? (props.multiple ? t('ui.fileUpload.dropFiles') : t('ui.fileUpload.dropFile')))
|
|
62
|
+
|
|
63
|
+
// The same check the file picker does, for dropped files: an extension
|
|
64
|
+
// (".csv"), an exact type ("text/csv") or a family ("image/*").
|
|
65
|
+
function allowed(file: File) {
|
|
66
|
+
if (!props.accept) return true
|
|
67
|
+
const name = file.name.toLowerCase()
|
|
68
|
+
const type = file.type.toLowerCase()
|
|
69
|
+
return props.accept.split(',').some((raw) => {
|
|
70
|
+
const rule = raw.trim().toLowerCase()
|
|
71
|
+
if (!rule) return false
|
|
72
|
+
if (rule.startsWith('.')) return name.endsWith(rule)
|
|
73
|
+
if (rule.endsWith('/*')) return type.startsWith(rule.slice(0, -1))
|
|
74
|
+
return type === rule
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function choose(list: FileList | null | undefined) {
|
|
79
|
+
const files = Array.from(list ?? []).filter(allowed)
|
|
80
|
+
if (!files.length) return
|
|
81
|
+
emit('select', props.multiple ? files : files.slice(0, 1))
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function open() {
|
|
85
|
+
if (!props.disabled) input.value?.click()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function onPick(event: Event) {
|
|
89
|
+
const target = event.target as HTMLInputElement
|
|
90
|
+
choose(target.files)
|
|
91
|
+
// Clear it, so choosing the same file again still counts.
|
|
92
|
+
target.value = ''
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Dragging over the area. A counter, not a flag: dragenter / dragleave fire
|
|
96
|
+
// for every child the pointer crosses.
|
|
97
|
+
const over = ref(0)
|
|
98
|
+
const dragging = computed(() => over.value > 0)
|
|
99
|
+
|
|
100
|
+
function onDragEnter() {
|
|
101
|
+
if (!props.disabled) over.value++
|
|
102
|
+
}
|
|
103
|
+
function onDragLeave() {
|
|
104
|
+
over.value = Math.max(0, over.value - 1)
|
|
105
|
+
}
|
|
106
|
+
function onDrop(event: DragEvent) {
|
|
107
|
+
over.value = 0
|
|
108
|
+
if (!props.disabled) choose(event.dataTransfer?.files)
|
|
109
|
+
}
|
|
110
|
+
</script>
|
|
111
|
+
|
|
112
|
+
<template>
|
|
113
|
+
<!-- One root: the drop area, or just a wrapper round the button. The real
|
|
114
|
+
file input sits inside it, hidden: the Button opens it. -->
|
|
115
|
+
<div
|
|
116
|
+
:data-disabled="disabled || undefined"
|
|
117
|
+
:class="cn(
|
|
118
|
+
drop
|
|
119
|
+
? ['flex flex-col items-center gap-3 rounded-lg border-2 border-dashed p-8 text-center transition-colors', dragging ? 'border-primary bg-primary/5' : 'border-ink/25', disabled && 'opacity-40']
|
|
120
|
+
: 'inline-flex',
|
|
121
|
+
props.class
|
|
122
|
+
)"
|
|
123
|
+
@dragenter.prevent="drop && onDragEnter()"
|
|
124
|
+
@dragover.prevent
|
|
125
|
+
@dragleave="drop && onDragLeave()"
|
|
126
|
+
@drop.prevent="drop && onDrop($event)"
|
|
127
|
+
>
|
|
128
|
+
<template v-if="drop">
|
|
129
|
+
<FileUp class="size-8 text-primary-fg" aria-hidden="true" />
|
|
130
|
+
<slot>
|
|
131
|
+
<p class="text-sm text-ink/65">{{ dropText }}</p>
|
|
132
|
+
</slot>
|
|
133
|
+
</template>
|
|
134
|
+
<Button :variant="variant" :size="size" :status="status" :loading-text="loadingText" :disabled="disabled" @click="open">
|
|
135
|
+
<template #leading><slot name="leading"><Upload /></slot></template>
|
|
136
|
+
{{ buttonLabel }}
|
|
137
|
+
</Button>
|
|
138
|
+
<input
|
|
139
|
+
ref="input"
|
|
140
|
+
type="file"
|
|
141
|
+
class="sr-only"
|
|
142
|
+
tabindex="-1"
|
|
143
|
+
aria-hidden="true"
|
|
144
|
+
:accept="accept"
|
|
145
|
+
:multiple="multiple"
|
|
146
|
+
:disabled="disabled"
|
|
147
|
+
@change="onPick"
|
|
148
|
+
/>
|
|
149
|
+
</div>
|
|
150
|
+
</template>
|
|
@@ -57,18 +57,26 @@ export const CustomLinks: Story = {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
/** No app name, version or build (e.g. a public page): just the links and the copyright. */
|
|
60
|
+
/**
|
|
61
|
+
* Without the Appearance dropdown (`themeSwitcher: false`). The Language
|
|
62
|
+
* dropdown shows by itself once the app has more than one language.
|
|
63
|
+
*/
|
|
64
|
+
export const NoSwitchers: Story = {
|
|
65
|
+
args: { themeSwitcher: false, languageSwitcher: false },
|
|
66
|
+
}
|
|
67
|
+
|
|
60
68
|
export const NoVersion: Story = {
|
|
61
69
|
args: { appName: undefined, version: undefined, build: undefined },
|
|
62
70
|
}
|
|
63
71
|
|
|
64
|
-
/** The slot `end`, after the copyright
|
|
72
|
+
/** The slot `end`, after the copyright, e.g. a link to the status page. */
|
|
65
73
|
export const WithEndSlot: Story = {
|
|
66
74
|
render: (args) => ({
|
|
67
75
|
components: { Footer },
|
|
68
76
|
setup: () => ({ args }),
|
|
69
77
|
template: `
|
|
70
78
|
<Footer v-bind="args">
|
|
71
|
-
<template #end><
|
|
79
|
+
<template #end><a href="https://example.com/status" class="underline underline-offset-2">Status</a></template>
|
|
72
80
|
</Footer>`,
|
|
73
81
|
}),
|
|
74
82
|
}
|
|
@@ -3,6 +3,8 @@ import { useI18n } from 'vue-i18n'
|
|
|
3
3
|
import { computed, type Component, type HTMLAttributes } from 'vue'
|
|
4
4
|
import { cn } from '../../lib/cn'
|
|
5
5
|
import Link from '../Link/Link.vue'
|
|
6
|
+
import LanguageSwitcher from '../LanguageSwitcher/LanguageSwitcher.vue'
|
|
7
|
+
import ThemeSwitcher from '../ThemeSwitcher/ThemeSwitcher.vue'
|
|
6
8
|
import { bareVersion, displayVersion, yearRange } from './footer'
|
|
7
9
|
import { kestonFooterLinks } from './links'
|
|
8
10
|
|
|
@@ -26,7 +28,11 @@ export interface FooterLink {
|
|
|
26
28
|
* apps), which follow, after a divider. The version comes from the app's package.json and the build
|
|
27
29
|
* from its CI: in Vite, put them in with `define` (see the spec).
|
|
28
30
|
*
|
|
29
|
-
*
|
|
31
|
+
* On the right too: a Language dropdown (when the app has more than one
|
|
32
|
+
* language) and an Appearance dropdown (Light / Dark / Match device). Turn
|
|
33
|
+
* either off with `languageSwitcher` / `themeSwitcher`.
|
|
34
|
+
*
|
|
35
|
+
* Slots: nav (instead of the whole link list), end (after the copyright).
|
|
30
36
|
*/
|
|
31
37
|
export interface FooterProps {
|
|
32
38
|
/** The links on the left. Default: `kestonFooterLinks` (Help & support, Privacy Policy, Terms & Conditions). */
|
|
@@ -47,6 +53,10 @@ export interface FooterProps {
|
|
|
47
53
|
year?: number
|
|
48
54
|
/** The first year, to show a range such as 2024–2026. */
|
|
49
55
|
since?: number
|
|
56
|
+
/** Show the Language dropdown. Default true; it only shows when the app has more than one language. */
|
|
57
|
+
languageSwitcher?: boolean
|
|
58
|
+
/** Show the Appearance dropdown (Light / Dark / Match device). Default true. */
|
|
59
|
+
themeSwitcher?: boolean
|
|
50
60
|
class?: HTMLAttributes['class']
|
|
51
61
|
/** Extra classes for the parts. */
|
|
52
62
|
ui?: {
|
|
@@ -55,12 +65,15 @@ export interface FooterProps {
|
|
|
55
65
|
}
|
|
56
66
|
}
|
|
57
67
|
|
|
58
|
-
const { t } = useI18n()
|
|
68
|
+
const { t, availableLocales } = useI18n()
|
|
69
|
+
const showLanguages = computed(() => props.languageSwitcher && availableLocales.length > 1)
|
|
59
70
|
const props = withDefaults(defineProps<FooterProps>(), {
|
|
60
71
|
links: () => kestonFooterLinks,
|
|
61
72
|
moreLinks: () => [],
|
|
62
73
|
linkComponent: undefined,
|
|
63
|
-
holder: 'The Keston Group'
|
|
74
|
+
holder: 'The Keston Group',
|
|
75
|
+
languageSwitcher: true,
|
|
76
|
+
themeSwitcher: true
|
|
64
77
|
})
|
|
65
78
|
|
|
66
79
|
defineSlots<{
|
|
@@ -102,7 +115,12 @@ function linkAttrs(link: FooterLink) {
|
|
|
102
115
|
</slot>
|
|
103
116
|
</nav>
|
|
104
117
|
|
|
105
|
-
<div :class="cn('flex flex-wrap items-center gap-x-3 gap-y-
|
|
118
|
+
<div :class="cn('flex flex-wrap items-center gap-x-3 gap-y-2 sm:justify-end', ui?.meta)">
|
|
119
|
+
<!-- Language and appearance: compact dropdowns, labelled for screen readers. -->
|
|
120
|
+
<div v-if="showLanguages || themeSwitcher" class="flex flex-wrap items-center gap-2">
|
|
121
|
+
<LanguageSwitcher v-if="showLanguages" hide-label class="w-44" />
|
|
122
|
+
<ThemeSwitcher v-if="themeSwitcher" hide-label class="w-44" />
|
|
123
|
+
</div>
|
|
106
124
|
<p v-if="appName || version || build" class="flex flex-wrap items-baseline gap-x-1.5">
|
|
107
125
|
<span v-if="appName" class="font-bold text-ink/80">{{ appName }}</span>
|
|
108
126
|
<!-- Shows as "v1.4.2" but reads as "Version 1.4.2". -->
|
|
@@ -3,6 +3,7 @@ import { Bell, Building2, ChevronRight } from "@lucide/vue";
|
|
|
3
3
|
import { ref } from "vue";
|
|
4
4
|
import Button from "../Button/Button.vue";
|
|
5
5
|
import Header, { type HeaderUser } from "./Header.vue";
|
|
6
|
+
import ThemeSwitcher from "../ThemeSwitcher/ThemeSwitcher.vue";
|
|
6
7
|
|
|
7
8
|
const user: HeaderUser = {
|
|
8
9
|
name: "Ben Smith",
|
|
@@ -94,6 +95,25 @@ export const CustomUserMenu: Story = {
|
|
|
94
95
|
}),
|
|
95
96
|
};
|
|
96
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Light / Dark / Match device in the account menu: ThemeSwitcher in the
|
|
100
|
+
* account-menu slot, in a row with the menu's divider and padding.
|
|
101
|
+
*/
|
|
102
|
+
export const WithThemeSwitcher: Story = {
|
|
103
|
+
render: (args) => ({
|
|
104
|
+
components: { Header, ThemeSwitcher },
|
|
105
|
+
setup: () => ({ args }),
|
|
106
|
+
template: `
|
|
107
|
+
<Header v-bind="args">
|
|
108
|
+
<template #account-menu>
|
|
109
|
+
<div class="border-t border-ink/10 px-4 py-3">
|
|
110
|
+
<ThemeSwitcher />
|
|
111
|
+
</div>
|
|
112
|
+
</template>
|
|
113
|
+
</Header>`,
|
|
114
|
+
}),
|
|
115
|
+
};
|
|
116
|
+
|
|
97
117
|
/**
|
|
98
118
|
* The organisation slot replaces the organisation heading at the top of the
|
|
99
119
|
* account menu, e.g. with a row that switches organisation. A divider sits
|
|
@@ -98,3 +98,22 @@ export const AutoSave: Story = {
|
|
|
98
98
|
export const WithErrorText: Story = { args: { errorText: "Add a note before saving" } };
|
|
99
99
|
|
|
100
100
|
export const Disabled: Story = { args: { disabled: true } };
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* With a link: Link (or Ctrl/⌘+K) opens a panel for the address. Only
|
|
104
|
+
* https://, mailto: and tel: are accepted; https:// addresses typed or pasted
|
|
105
|
+
* become links by themselves. Put the cursor in the link to change or remove it.
|
|
106
|
+
*/
|
|
107
|
+
export const WithLink: Story = {
|
|
108
|
+
args: { label: "Sign-in page text" },
|
|
109
|
+
render: (args) => ({
|
|
110
|
+
components: { InputRichText },
|
|
111
|
+
setup: () => ({
|
|
112
|
+
args,
|
|
113
|
+
value: ref(
|
|
114
|
+
'<p><strong>New to StockTrack?</strong> Use your <em>work</em> email. For help, see the <a href="https://example.com/help">IT help centre</a> or call <a href="tel:08000000000">0800 000 0000</a>.</p>',
|
|
115
|
+
),
|
|
116
|
+
}),
|
|
117
|
+
template: `<div class="max-w-xl"><InputRichText v-bind="args" v-model="value" /></div>`,
|
|
118
|
+
}),
|
|
119
|
+
};
|
|
@@ -10,21 +10,30 @@ import HardBreak from '@tiptap/extension-hard-break'
|
|
|
10
10
|
import Bold from '@tiptap/extension-bold'
|
|
11
11
|
import Italic from '@tiptap/extension-italic'
|
|
12
12
|
import Underline from '@tiptap/extension-underline'
|
|
13
|
+
import Link from '@tiptap/extension-link'
|
|
13
14
|
import { Placeholder, UndoRedo } from '@tiptap/extensions'
|
|
14
|
-
import { Bold as BoldIcon, Italic as ItalicIcon, Underline as UnderlineIcon } from '@lucide/vue'
|
|
15
|
+
import { Bold as BoldIcon, Italic as ItalicIcon, Link as LinkIcon, Underline as UnderlineIcon } from '@lucide/vue'
|
|
15
16
|
import { cn } from '../../lib/cn'
|
|
16
17
|
import FormField, { type FormFieldStatus } from '../FormField/FormField.vue'
|
|
17
18
|
import Button from '../Button/Button.vue'
|
|
18
19
|
import Tooltip from '../Tooltip/Tooltip.vue'
|
|
20
|
+
import Popover from '../Popover/Popover.vue'
|
|
21
|
+
import Input from '../Input/Input.vue'
|
|
19
22
|
|
|
20
23
|
/**
|
|
21
|
-
* Several lines of text with bold, italic and
|
|
22
|
-
* student or
|
|
23
|
-
* colours, so
|
|
24
|
-
*
|
|
24
|
+
* Several lines of text with bold, italic, underline and links, e.g. notes
|
|
25
|
+
* on a student, or a company's sign-in page text with a link to its help
|
|
26
|
+
* pages. No headings, lists or colours, so text looks the same everywhere.
|
|
27
|
+
* Pasted text keeps those and loses everything else.
|
|
25
28
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
29
|
+
* Links: the Link button or Ctrl/⌘+K opens a small panel for the address.
|
|
30
|
+
* Only https://, mailto: and tel: addresses are allowed (never javascript:
|
|
31
|
+
* or plain http). Typed and pasted https:// addresses become links too.
|
|
32
|
+
* Links open in the same tab, so nobody is sent to a new tab unannounced.
|
|
33
|
+
*
|
|
34
|
+
* The value is HTML using only <p>, <br>, <strong>, <em>, <u> and
|
|
35
|
+
* <a href rel>. Sanitise it on the server like any HTML from a browser
|
|
36
|
+
* before showing it elsewhere.
|
|
28
37
|
*/
|
|
29
38
|
export interface InputRichTextProps {
|
|
30
39
|
/** Visible label. Always set it: it is also the editor's accessible name. */
|
|
@@ -55,6 +64,9 @@ const props = withDefaults(defineProps<InputRichTextProps>(), {
|
|
|
55
64
|
/** The text as HTML; an empty string when there's nothing in it. */
|
|
56
65
|
const model = defineModel<string>({ default: '' })
|
|
57
66
|
|
|
67
|
+
// Addresses a link may have: secure web pages, email and phone.
|
|
68
|
+
const SAFE_LINK = /^(https:\/\/[^\s]+|mailto:[^\s]+|tel:[+\d][\d\s()-]*)$/i
|
|
69
|
+
|
|
58
70
|
// An empty editor still holds an empty paragraph; report that as ''.
|
|
59
71
|
const html = () => (editor.value?.isEmpty ? '' : (editor.value?.getHTML() ?? ''))
|
|
60
72
|
|
|
@@ -69,6 +81,25 @@ const editor = useEditor({
|
|
|
69
81
|
Bold,
|
|
70
82
|
Italic,
|
|
71
83
|
Underline,
|
|
84
|
+
Link.extend({
|
|
85
|
+
// Ctrl/⌘+K opens the link panel, as in most editors.
|
|
86
|
+
addKeyboardShortcuts: () => ({
|
|
87
|
+
'Mod-k': () => {
|
|
88
|
+
openLink()
|
|
89
|
+
return true
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
}).configure({
|
|
93
|
+
openOnClick: false,
|
|
94
|
+
autolink: true,
|
|
95
|
+
linkOnPaste: true,
|
|
96
|
+
defaultProtocol: 'https',
|
|
97
|
+
protocols: ['mailto', 'tel'],
|
|
98
|
+
isAllowedUri: (url) => SAFE_LINK.test(url),
|
|
99
|
+
shouldAutoLink: (url) => /^https:\/\//i.test(url),
|
|
100
|
+
// Same tab, no referrer; no target, so no unannounced new tabs.
|
|
101
|
+
HTMLAttributes: { rel: 'noopener noreferrer nofollow', target: null, class: null }
|
|
102
|
+
}),
|
|
72
103
|
UndoRedo,
|
|
73
104
|
Placeholder.configure({ placeholder: () => props.placeholder ?? '' })
|
|
74
105
|
],
|
|
@@ -82,10 +113,54 @@ const editor = useEditor({
|
|
|
82
113
|
|
|
83
114
|
// Which formats are on at the cursor (or for the next text typed), for the
|
|
84
115
|
// toolbar's pressed state. Tiptap's isActive() isn't reactive by itself.
|
|
85
|
-
type Mark = 'bold' | 'italic' | 'underline'
|
|
86
|
-
const active = ref<Record<Mark, boolean>>({ bold: false, italic: false, underline: false })
|
|
116
|
+
type Mark = 'bold' | 'italic' | 'underline' | 'link'
|
|
117
|
+
const active = ref<Record<Mark, boolean>>({ bold: false, italic: false, underline: false, link: false })
|
|
87
118
|
function updateActive(e: Editor) {
|
|
88
|
-
active.value = {
|
|
119
|
+
active.value = {
|
|
120
|
+
bold: e.isActive('bold'),
|
|
121
|
+
italic: e.isActive('italic'),
|
|
122
|
+
underline: e.isActive('underline'),
|
|
123
|
+
link: e.isActive('link')
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// ── Links ───────────────────────────────────────────────────────────────────
|
|
128
|
+
// A small panel under the Link button: the address, then Add / Update and
|
|
129
|
+
// (on a link) Remove. With text selected, the address is put on that text;
|
|
130
|
+
// with nothing selected, the address itself is added as the link's text.
|
|
131
|
+
const linkOpen = ref(false)
|
|
132
|
+
const linkHref = ref('')
|
|
133
|
+
const linkError = ref<string>()
|
|
134
|
+
// Set here rather than in the template: vue-tsc misreads a literal "//" in
|
|
135
|
+
// a template attribute inside nested slots as a comment.
|
|
136
|
+
const linkPlaceholder = 'https://'
|
|
137
|
+
|
|
138
|
+
function openLink() {
|
|
139
|
+
if (props.disabled || !editor.value) return
|
|
140
|
+
linkHref.value = (editor.value.getAttributes('link').href as string | undefined) ?? ''
|
|
141
|
+
linkError.value = undefined
|
|
142
|
+
linkOpen.value = true
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function applyLink() {
|
|
146
|
+
const e = editor.value
|
|
147
|
+
if (!e) return
|
|
148
|
+
const href = linkHref.value.trim()
|
|
149
|
+
if (!SAFE_LINK.test(href)) {
|
|
150
|
+
linkError.value = t('ui.inputRichText.linkInvalid')
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
if (e.state.selection.empty && !e.isActive('link')) {
|
|
154
|
+
e.chain().focus().insertContent({ type: 'text', text: href, marks: [{ type: 'link', attrs: { href } }] }).run()
|
|
155
|
+
} else {
|
|
156
|
+
e.chain().focus().extendMarkRange('link').setLink({ href }).run()
|
|
157
|
+
}
|
|
158
|
+
linkOpen.value = false
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function removeLink() {
|
|
162
|
+
editor.value?.chain().focus().extendMarkRange('link').unsetLink().run()
|
|
163
|
+
linkOpen.value = false
|
|
89
164
|
}
|
|
90
165
|
|
|
91
166
|
// A value set from outside (e.g. a form reset) replaces the text; the
|
|
@@ -145,6 +220,7 @@ const editable = [
|
|
|
145
220
|
// 16px on touch screens, so iPhone doesn't zoom in when it's tapped.
|
|
146
221
|
'min-h-24 max-h-80 overflow-y-auto px-3 py-2 text-sm pointer-coarse:text-base outline-none cursor-text',
|
|
147
222
|
'[&_p+p]:mt-2',
|
|
223
|
+
'[&_a]:text-primary-fg [&_a]:underline [&_a]:underline-offset-2',
|
|
148
224
|
'[&_p.is-editor-empty:first-child]:before:pointer-events-none [&_p.is-editor-empty:first-child]:before:float-left',
|
|
149
225
|
'[&_p.is-editor-empty:first-child]:before:h-0 [&_p.is-editor-empty:first-child]:before:text-ink/55',
|
|
150
226
|
'[&_p.is-editor-empty:first-child]:before:content-[attr(data-placeholder)]'
|
|
@@ -163,7 +239,7 @@ const editable = [
|
|
|
163
239
|
:disabled="disabled"
|
|
164
240
|
:class="props.class"
|
|
165
241
|
>
|
|
166
|
-
<template #default="
|
|
242
|
+
<template #default="field">
|
|
167
243
|
<!-- Fills FormField's box: negative margins cancel its padding, so the
|
|
168
244
|
toolbar's line and the text area run to the edges. -->
|
|
169
245
|
<div class="-mx-3 -my-2 flex min-w-0 flex-1 flex-col self-stretch">
|
|
@@ -171,7 +247,7 @@ const editable = [
|
|
|
171
247
|
<div
|
|
172
248
|
role="toolbar"
|
|
173
249
|
:aria-label="t('ui.inputRichText.formattingFor', { label })"
|
|
174
|
-
:aria-controls="
|
|
250
|
+
:aria-controls="field.id"
|
|
175
251
|
class="flex items-center gap-0.5 border-b border-ink/10 px-1.5 py-1 [&_svg]:opacity-100!"
|
|
176
252
|
@mousedown.prevent
|
|
177
253
|
>
|
|
@@ -190,8 +266,48 @@ const editable = [
|
|
|
190
266
|
<component :is="mark.icon" />
|
|
191
267
|
</Button>
|
|
192
268
|
</Tooltip>
|
|
269
|
+
<!-- Link: opens a panel for the address (also Ctrl/⌘+K). -->
|
|
270
|
+
<Popover v-model:open="linkOpen" :content="{ side: 'bottom', align: 'start', sideOffset: 6 }">
|
|
271
|
+
<template #default>
|
|
272
|
+
<Tooltip :text="`${t('ui.inputRichText.link')} (${mod}K)`" :disabled="disabled">
|
|
273
|
+
<Button
|
|
274
|
+
variant="ghost"
|
|
275
|
+
size="sm"
|
|
276
|
+
square
|
|
277
|
+
:aria-label="t('ui.inputRichText.link')"
|
|
278
|
+
aria-haspopup="dialog"
|
|
279
|
+
:aria-keyshortcuts="`${isMac ? 'Meta' : 'Control'}+K`"
|
|
280
|
+
:disabled="disabled"
|
|
281
|
+
:class="active.link && 'bg-selected'"
|
|
282
|
+
@click.prevent="openLink"
|
|
283
|
+
>
|
|
284
|
+
<LinkIcon />
|
|
285
|
+
</Button>
|
|
286
|
+
</Tooltip>
|
|
287
|
+
</template>
|
|
288
|
+
<template #content>
|
|
289
|
+
<form class="flex w-72 flex-col gap-3 p-3" novalidate @submit.prevent="applyLink" @mousedown.stop>
|
|
290
|
+
<Input
|
|
291
|
+
v-model="linkHref"
|
|
292
|
+
:label="t('ui.inputRichText.linkAddress')"
|
|
293
|
+
type="url"
|
|
294
|
+
inputmode="url"
|
|
295
|
+
:placeholder="linkPlaceholder"
|
|
296
|
+
autocomplete="off"
|
|
297
|
+
spellcheck="false"
|
|
298
|
+
:error-text="linkError"
|
|
299
|
+
/>
|
|
300
|
+
<div class="flex justify-end gap-2">
|
|
301
|
+
<Button v-if="active.link" size="sm" variant="ghost" @click="removeLink">{{ t('ui.inputRichText.removeLink') }}</Button>
|
|
302
|
+
<Button size="sm" variant="primary" type="submit">
|
|
303
|
+
{{ active.link ? t('ui.inputRichText.updateLink') : t('ui.inputRichText.addLink') }}
|
|
304
|
+
</Button>
|
|
305
|
+
</div>
|
|
306
|
+
</form>
|
|
307
|
+
</template>
|
|
308
|
+
</Popover>
|
|
193
309
|
</div>
|
|
194
|
-
<EditorAttributes :ids="
|
|
310
|
+
<EditorAttributes :ids="field" />
|
|
195
311
|
<EditorContent :editor="editor" :class="cn('min-w-0', disabled && 'cursor-not-allowed [&_*]:cursor-not-allowed')" />
|
|
196
312
|
</div>
|
|
197
313
|
</template>
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/vue3-vite'
|
|
2
|
+
import LineChart from './LineChart.vue'
|
|
3
|
+
import Card from '../Card/Card.vue'
|
|
4
|
+
|
|
5
|
+
const weeks = Array.from({ length: 12 }, (_, i) => ({
|
|
6
|
+
label: `W${i + 1}`,
|
|
7
|
+
fullLabel: `Week ${i + 1}`,
|
|
8
|
+
values: {
|
|
9
|
+
thisYear: [12, 18, 25, 31, 44, 52, 61, 58, 70, 83, 91, 104][i],
|
|
10
|
+
lastYear: [10, 14, 20, 29, 35, 41, 50, 55, 60, 66, 73, 80][i],
|
|
11
|
+
},
|
|
12
|
+
}))
|
|
13
|
+
|
|
14
|
+
const meta = {
|
|
15
|
+
title: 'Components/Data/LineChart',
|
|
16
|
+
component: LineChart,
|
|
17
|
+
tags: ['autodocs'],
|
|
18
|
+
args: {
|
|
19
|
+
label: 'Applications per week',
|
|
20
|
+
categoryLabel: 'Week',
|
|
21
|
+
data: weeks,
|
|
22
|
+
series: [
|
|
23
|
+
{ key: 'thisYear', label: 'This year' },
|
|
24
|
+
{ key: 'lastYear', label: 'Last year' },
|
|
25
|
+
],
|
|
26
|
+
area: false,
|
|
27
|
+
},
|
|
28
|
+
render: (args) => ({
|
|
29
|
+
components: { LineChart, Card },
|
|
30
|
+
setup: () => ({ args }),
|
|
31
|
+
template: `
|
|
32
|
+
<Card title="Applications per week" description="This admissions round against last year's." class="max-w-2xl">
|
|
33
|
+
<LineChart v-bind="args" />
|
|
34
|
+
</Card>`,
|
|
35
|
+
}),
|
|
36
|
+
} satisfies Meta<typeof LineChart>
|
|
37
|
+
|
|
38
|
+
export default meta
|
|
39
|
+
type Story = StoryObj<typeof meta>
|
|
40
|
+
|
|
41
|
+
/** Two lines to compare. Hover for the values at a week; "Show as table" for all of them. */
|
|
42
|
+
export const Default: Story = {}
|
|
43
|
+
|
|
44
|
+
/** One line with a light wash under it. */
|
|
45
|
+
export const OneSeriesWithArea: Story = {
|
|
46
|
+
args: { series: [{ key: 'thisYear', label: 'Applications' }], area: true },
|
|
47
|
+
}
|