socle-ui 0.0.0-stage → 0.4.2
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/CHANGELOG.md +167 -0
- package/LICENSE +21 -0
- package/README.md +255 -2
- package/SocleDatePicker.vue +659 -0
- package/SocleDialog.vue +250 -0
- package/SocleField.vue +72 -0
- package/SocleInput.vue +164 -0
- package/SoclePopover.vue +171 -0
- package/SocleSelect.vue +331 -0
- package/SocleTableHead.vue +336 -0
- package/SocleTabs.vue +226 -0
- package/classes.ts +18 -0
- package/index.ts +13 -0
- package/package.json +51 -4
- package/socle.css +356 -0
- package/table.ts +120 -0
- package/tailwind.css +190 -0
package/SocleDialog.vue
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
SocleDialog.vue
|
|
3
|
+
Boîte de dialogue déplaçable : on la déplace en faisant glisser son titre.
|
|
4
|
+
Basée sur l'élément natif <dialog> (top layer, focus piégé en mode modal, Échap natif).
|
|
5
|
+
Prérequis : Vue >= 3.5 (useId), Tailwind CSS v4.
|
|
6
|
+
|
|
7
|
+
<SocleDialog v-model="open" title="Modifier le profil" width="30rem">
|
|
8
|
+
<p>Contenu de la boîte…</p>
|
|
9
|
+
<template #footer>
|
|
10
|
+
<button class="socle-button" @click="open = false">Annuler</button>
|
|
11
|
+
<button class="socle-button-primary">Enregistrer</button>
|
|
12
|
+
</template>
|
|
13
|
+
</SocleDialog>
|
|
14
|
+
|
|
15
|
+
- v-model : ouvre / ferme la boîte.
|
|
16
|
+
- modal : true (défaut) → showModal() avec fond assombri ; false → show(), la page reste utilisable.
|
|
17
|
+
- Déplacement : glisser le titre (souris, tactile, stylet). Double-clic sur le titre = recentrer.
|
|
18
|
+
La boîte reste toujours entièrement dans la fenêtre.
|
|
19
|
+
- Slots : défaut (contenu), `title` (titre riche), `footer` (boutons d'action).
|
|
20
|
+
En mode non modal, la boîte n'est pas dans le top layer : elle est passée en position fixed (centrée dans la fenêtre, z-50) pour garder le même comportement visuel qu'en modal.
|
|
21
|
+
|
|
22
|
+
-->
|
|
23
|
+
<script setup lang="ts">
|
|
24
|
+
import { computed, onBeforeUnmount, onMounted, ref, useId, watch } from 'vue'
|
|
25
|
+
import './socle.css'
|
|
26
|
+
|
|
27
|
+
interface Labels {
|
|
28
|
+
close: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const defaultLabels: Labels = {
|
|
32
|
+
close: 'Fermer',
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const props = withDefaults(
|
|
36
|
+
defineProps<{
|
|
37
|
+
title?: string
|
|
38
|
+
/** Modal : fond assombri, reste de la page inerte. Non modal : la page reste utilisable. */
|
|
39
|
+
modal?: boolean
|
|
40
|
+
/** (modal uniquement) Un clic sur le fond assombri ferme la boîte. */
|
|
41
|
+
closeOnBackdrop?: boolean
|
|
42
|
+
/** Largeur CSS de la boîte (plafonnée à la largeur de la fenêtre). */
|
|
43
|
+
width?: string
|
|
44
|
+
labels?: Partial<Labels>
|
|
45
|
+
}>(),
|
|
46
|
+
{ modal: true, closeOnBackdrop: true, width: '28rem' },
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
const open = defineModel<boolean>({ default: false })
|
|
50
|
+
|
|
51
|
+
defineSlots<{
|
|
52
|
+
default?(): unknown
|
|
53
|
+
title?(): unknown
|
|
54
|
+
footer?(): unknown
|
|
55
|
+
}>()
|
|
56
|
+
|
|
57
|
+
const L = computed<Labels>(() => ({ ...defaultLabels, ...props.labels }))
|
|
58
|
+
|
|
59
|
+
/* ---------- Identifiants ---------- */
|
|
60
|
+
const uid = useId()
|
|
61
|
+
const titleId = `${uid}-title`
|
|
62
|
+
|
|
63
|
+
/* ---------- Ouverture / fermeture ---------- */
|
|
64
|
+
const dialog = ref<HTMLDialogElement | null>(null)
|
|
65
|
+
|
|
66
|
+
function openDialog() {
|
|
67
|
+
const el = dialog.value
|
|
68
|
+
if (!el || el.open) return
|
|
69
|
+
pos.value = { x: 0, y: 0 } // toujours rouvert au centre
|
|
70
|
+
props.modal ? el.showModal() : el.show()
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function closeDialog() {
|
|
74
|
+
if (dialog.value?.open) dialog.value.close()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// flush: 'post' → le <dialog> est bien dans le DOM quand on réagit.
|
|
78
|
+
watch(open, (v) => (v ? openDialog() : closeDialog()), { flush: 'post' })
|
|
79
|
+
|
|
80
|
+
/** Événement natif "close" (Échap, form method="dialog", close()) : on resynchronise le v-model.
|
|
81
|
+
* L'événement est asynchrone : s'il arrive alors que la boîte a été rouverte entre-temps
|
|
82
|
+
* (bascule modal ↔ non modal), on l'ignore. */
|
|
83
|
+
function onClose() {
|
|
84
|
+
if (dialog.value?.open) return
|
|
85
|
+
open.value = false
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Changement de `modal` pendant que la boîte est ouverte : on la rouvre dans le bon mode, sans la recentrer. */
|
|
89
|
+
watch(
|
|
90
|
+
() => props.modal,
|
|
91
|
+
(modal) => {
|
|
92
|
+
const el = dialog.value
|
|
93
|
+
if (!el?.open) return
|
|
94
|
+
el.close()
|
|
95
|
+
modal ? el.showModal() : el.show()
|
|
96
|
+
},
|
|
97
|
+
{ flush: 'post' },
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
/** En mode non modal, le navigateur ne ferme pas avec Échap : on le fait. */
|
|
101
|
+
function onEscape(e: KeyboardEvent) {
|
|
102
|
+
if (props.modal) return
|
|
103
|
+
e.preventDefault()
|
|
104
|
+
open.value = false
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Clic sur le fond : n'accepte que si le clic a AUSSI commencé sur le fond
|
|
108
|
+
// (évite de fermer quand on termine une sélection de texte en dehors).
|
|
109
|
+
// `e.target === dialog` ne suffit pas : la bordure de la boîte a aussi le <dialog> pour cible,
|
|
110
|
+
// on vérifie donc que le pointeur est réellement hors du rectangle de la boîte.
|
|
111
|
+
function isOnBackdrop(e: MouseEvent) {
|
|
112
|
+
const el = dialog.value
|
|
113
|
+
if (!el || e.target !== el) return false
|
|
114
|
+
const r = el.getBoundingClientRect()
|
|
115
|
+
return e.clientX < r.left || e.clientX > r.right || e.clientY < r.top || e.clientY > r.bottom
|
|
116
|
+
}
|
|
117
|
+
let downOnBackdrop = false
|
|
118
|
+
function onDialogPointerDown(e: PointerEvent) {
|
|
119
|
+
downOnBackdrop = isOnBackdrop(e)
|
|
120
|
+
}
|
|
121
|
+
function onDialogClick(e: MouseEvent) {
|
|
122
|
+
if (props.modal && props.closeOnBackdrop && downOnBackdrop && isOnBackdrop(e)) {
|
|
123
|
+
open.value = false
|
|
124
|
+
}
|
|
125
|
+
downOnBackdrop = false
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/* ---------- Déplacement ---------- */
|
|
129
|
+
/** Décalage par rapport à la position centrée d'origine (appliqué via la propriété CSS `translate`). */
|
|
130
|
+
const pos = ref({ x: 0, y: 0 })
|
|
131
|
+
const dragging = ref(false)
|
|
132
|
+
|
|
133
|
+
const clamp = (v: number, lo: number, hi: number) => Math.min(Math.max(v, lo), hi)
|
|
134
|
+
|
|
135
|
+
/** Plage de décalage autorisée pour que la boîte reste entièrement visible. */
|
|
136
|
+
function limits(rect: DOMRect) {
|
|
137
|
+
const vw = document.documentElement.clientWidth
|
|
138
|
+
const vh = document.documentElement.clientHeight
|
|
139
|
+
return { minX: -rect.left, maxX: vw - rect.right, minY: -rect.top, maxY: vh - rect.bottom }
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
let drag: { startX: number; startY: number; originX: number; originY: number; rect: DOMRect } | null = null
|
|
143
|
+
|
|
144
|
+
function onHandlePointerDown(e: PointerEvent) {
|
|
145
|
+
// Bouton principal uniquement ; le bouton de fermeture garde son propre comportement.
|
|
146
|
+
if (e.button !== 0 || (e.target as HTMLElement).closest('button') || !dialog.value) return
|
|
147
|
+
drag = {
|
|
148
|
+
startX: e.clientX,
|
|
149
|
+
startY: e.clientY,
|
|
150
|
+
originX: pos.value.x,
|
|
151
|
+
originY: pos.value.y,
|
|
152
|
+
rect: dialog.value.getBoundingClientRect(),
|
|
153
|
+
}
|
|
154
|
+
;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
|
|
155
|
+
dragging.value = true
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function onHandlePointerMove(e: PointerEvent) {
|
|
159
|
+
if (!drag) return
|
|
160
|
+
const l = limits(drag.rect)
|
|
161
|
+
const dx = clamp(e.clientX - drag.startX, l.minX, l.maxX)
|
|
162
|
+
const dy = clamp(e.clientY - drag.startY, l.minY, l.maxY)
|
|
163
|
+
pos.value = { x: drag.originX + dx, y: drag.originY + dy }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function endDrag(e: PointerEvent) {
|
|
167
|
+
if (!drag) return
|
|
168
|
+
drag = null
|
|
169
|
+
dragging.value = false
|
|
170
|
+
const el = e.currentTarget as HTMLElement
|
|
171
|
+
if (el.hasPointerCapture(e.pointerId)) el.releasePointerCapture(e.pointerId)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Déplacement relatif (redimensionnement de la fenêtre), toujours borné à la fenêtre. */
|
|
175
|
+
function moveBy(dx: number, dy: number) {
|
|
176
|
+
if (!dialog.value?.open) return
|
|
177
|
+
const l = limits(dialog.value.getBoundingClientRect())
|
|
178
|
+
pos.value = {
|
|
179
|
+
x: pos.value.x + clamp(dx, l.minX, l.maxX),
|
|
180
|
+
y: pos.value.y + clamp(dy, l.minY, l.maxY),
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function resetPosition() {
|
|
185
|
+
pos.value = { x: 0, y: 0 }
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const onResize = () => moveBy(0, 0)
|
|
189
|
+
|
|
190
|
+
onMounted(() => {
|
|
191
|
+
window.addEventListener('resize', onResize)
|
|
192
|
+
if (open.value) openDialog()
|
|
193
|
+
})
|
|
194
|
+
onBeforeUnmount(() => window.removeEventListener('resize', onResize))
|
|
195
|
+
</script>
|
|
196
|
+
|
|
197
|
+
<template>
|
|
198
|
+
<dialog
|
|
199
|
+
ref="dialog"
|
|
200
|
+
:aria-labelledby="title || $slots.title ? titleId : undefined"
|
|
201
|
+
:class="!modal && 'fixed inset-0 z-50'"
|
|
202
|
+
class="socle-dialog m-auto max-h-[min(40rem,calc(100dvh-2rem))] flex-col rounded-socle-lg border border-socle-border-subtle bg-socle-surface p-0 text-socle-text shadow-xl open:flex"
|
|
203
|
+
:style="{
|
|
204
|
+
width,
|
|
205
|
+
maxWidth: 'calc(100vw - 2rem)',
|
|
206
|
+
translate: `${pos.x}px ${pos.y}px`,
|
|
207
|
+
}"
|
|
208
|
+
@close="onClose"
|
|
209
|
+
@keydown.esc="onEscape"
|
|
210
|
+
@pointerdown="onDialogPointerDown"
|
|
211
|
+
@click="onDialogClick"
|
|
212
|
+
>
|
|
213
|
+
<!-- Barre de titre = poignée de déplacement -->
|
|
214
|
+
<div
|
|
215
|
+
class="flex shrink-0 touch-none select-none items-center justify-between gap-2 rounded-t-socle-lg border-b border-socle-border-subtle bg-socle-surface-muted py-2 pl-4 pr-2"
|
|
216
|
+
:class="dragging ? 'cursor-grabbing' : 'cursor-grab'"
|
|
217
|
+
@pointerdown="onHandlePointerDown"
|
|
218
|
+
@pointermove="onHandlePointerMove"
|
|
219
|
+
@pointerup="endDrag"
|
|
220
|
+
@pointercancel="endDrag"
|
|
221
|
+
@dblclick="resetPosition"
|
|
222
|
+
>
|
|
223
|
+
<h2 :id="titleId" class="truncate text-sm font-semibold">
|
|
224
|
+
<slot name="title">{{ title }}</slot>
|
|
225
|
+
</h2>
|
|
226
|
+
|
|
227
|
+
<button
|
|
228
|
+
type="button"
|
|
229
|
+
:aria-label="L.close"
|
|
230
|
+
class="flex size-8 shrink-0 cursor-pointer items-center justify-center rounded-socle-sm text-socle-text-muted outline-none hover:bg-socle-border-subtle/70 focus-visible:ring-2 focus-visible:ring-socle-accent/50"
|
|
231
|
+
@click="open = false"
|
|
232
|
+
>
|
|
233
|
+
<svg class="size-4" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
|
234
|
+
<path d="m5 5 10 10M15 5 5 15" />
|
|
235
|
+
</svg>
|
|
236
|
+
</button>
|
|
237
|
+
</div>
|
|
238
|
+
|
|
239
|
+
<div class="min-h-0 flex-1 overflow-y-auto px-4 py-4">
|
|
240
|
+
<slot />
|
|
241
|
+
</div>
|
|
242
|
+
|
|
243
|
+
<footer
|
|
244
|
+
v-if="$slots.footer"
|
|
245
|
+
class="flex shrink-0 items-center justify-end gap-2 rounded-b-socle-lg border-t border-socle-border-subtle px-4 py-3"
|
|
246
|
+
>
|
|
247
|
+
<slot name="footer" />
|
|
248
|
+
</footer>
|
|
249
|
+
</dialog>
|
|
250
|
+
</template>
|
package/SocleField.vue
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
SocleField.vue
|
|
3
|
+
Habillage commun d'un champ de formulaire : label (avec astérisque si requis),
|
|
4
|
+
zone d'erreur annoncée poliment et texte d'aide. Le contrôle lui-même va dans le slot.
|
|
5
|
+
Prérequis : Vue >= 3.5 (useId), Tailwind CSS v4.
|
|
6
|
+
|
|
7
|
+
<SocleField control-id="pseudo" label="Pseudo" :error="err" v-slot="{ describedBy }">
|
|
8
|
+
<textarea id="pseudo" :aria-describedby="describedBy" :aria-invalid="err ? true : undefined" />
|
|
9
|
+
</SocleField>
|
|
10
|
+
|
|
11
|
+
- controlId : id du contrôle, cible du <label for>.
|
|
12
|
+
- Slot défaut : reçoit `labelId` (pour aria-labelledby) et `describedBy` (pour aria-describedby).
|
|
13
|
+
- @label-click : clic sur le label, pour les contrôles qui ne doivent pas être activés
|
|
14
|
+
par ce clic (ex. un <button> : appeler preventDefault() puis donner le focus).
|
|
15
|
+
- Attributs : class, style et écouteurs non déclarés vont sur le conteneur.
|
|
16
|
+
|
|
17
|
+
-->
|
|
18
|
+
<script setup lang="ts">
|
|
19
|
+
import { computed, ref, useId } from 'vue'
|
|
20
|
+
|
|
21
|
+
const props = defineProps<{
|
|
22
|
+
/** id du contrôle associé au label. */
|
|
23
|
+
controlId: string
|
|
24
|
+
label?: string
|
|
25
|
+
required?: boolean
|
|
26
|
+
/** Texte d'aide sous le champ (remplacé par `error` si présent). */
|
|
27
|
+
hint?: string
|
|
28
|
+
/** Message d'erreur. */
|
|
29
|
+
error?: string
|
|
30
|
+
}>()
|
|
31
|
+
|
|
32
|
+
const emit = defineEmits<{
|
|
33
|
+
labelClick: [event: MouseEvent]
|
|
34
|
+
}>()
|
|
35
|
+
|
|
36
|
+
defineSlots<{
|
|
37
|
+
default(props: { labelId: string; describedBy: string | undefined }): unknown
|
|
38
|
+
}>()
|
|
39
|
+
|
|
40
|
+
const uid = useId()
|
|
41
|
+
const labelId = `${uid}-label`
|
|
42
|
+
const hintId = `${uid}-hint`
|
|
43
|
+
const errorId = `${uid}-error`
|
|
44
|
+
|
|
45
|
+
const describedBy = computed(() => (props.error ? errorId : props.hint ? hintId : undefined))
|
|
46
|
+
|
|
47
|
+
/** Conteneur, exposé pour les tests d'appartenance (focus, clic extérieur). */
|
|
48
|
+
const root = ref<HTMLElement | null>(null)
|
|
49
|
+
defineExpose({ root })
|
|
50
|
+
</script>
|
|
51
|
+
|
|
52
|
+
<template>
|
|
53
|
+
<div ref="root" class="w-full">
|
|
54
|
+
<label
|
|
55
|
+
v-if="label"
|
|
56
|
+
:id="labelId"
|
|
57
|
+
:for="controlId"
|
|
58
|
+
class="mb-1.5 block text-sm font-medium text-socle-text-muted"
|
|
59
|
+
@click="emit('labelClick', $event)"
|
|
60
|
+
>
|
|
61
|
+
{{ label }}
|
|
62
|
+
<span v-if="required" class="text-socle-danger" aria-hidden="true">*</span>
|
|
63
|
+
</label>
|
|
64
|
+
|
|
65
|
+
<slot :labelId="labelId" :describedBy="describedBy" />
|
|
66
|
+
|
|
67
|
+
<!-- Zone d'erreur toujours présente : une région live "polite" doit exister avant que son contenu change.
|
|
68
|
+
Moins intrusif que role="alert", qui interrompt et empile les annonces de tous les champs à l'envoi. -->
|
|
69
|
+
<p :id="errorId" aria-live="polite" class="text-xs text-socle-danger" :class="{ 'mt-1.5': error }">{{ error }}</p>
|
|
70
|
+
<p v-if="hint && !error" :id="hintId" class="mt-1.5 text-xs text-socle-text-subtle">{{ hint }}</p>
|
|
71
|
+
</div>
|
|
72
|
+
</template>
|
package/SocleInput.vue
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Prérequis : Vue >= 3.5 (useId, defineModel), Tailwind CSS v4 avec `@import "socle-ui/tailwind.css"` (utilitaire socle-field-box).
|
|
3
|
+
|
|
4
|
+
<SocleInput
|
|
5
|
+
v-model.trim="email"
|
|
6
|
+
label="E-mail"
|
|
7
|
+
type="email"
|
|
8
|
+
name="email"
|
|
9
|
+
placeholder="nom@exemple.fr"
|
|
10
|
+
hint="Nous ne l'utiliserons que pour vous répondre."
|
|
11
|
+
:error="errors.email"
|
|
12
|
+
required
|
|
13
|
+
autocomplete="email"
|
|
14
|
+
/>
|
|
15
|
+
|
|
16
|
+
- v-model : texte saisi. Modificateurs Vue supportés : .trim, .number, .lazy.
|
|
17
|
+
- Attributs : tout attribut non déclaré (autocomplete, maxlength, min, step, inputmode, @keydown…) est transmis à l'<input>, pas au conteneur.
|
|
18
|
+
- Slots : `prefix` / `suffix` (icône, unité, bouton) à l'intérieur du champ.
|
|
19
|
+
- Erreur : la prop `error` colore le champ, affiche le message (annoncé poliment) et pose aria-invalid.
|
|
20
|
+
|
|
21
|
+
-->
|
|
22
|
+
<script setup lang="ts">
|
|
23
|
+
import { computed, onMounted, ref, useAttrs, useId, watch } from 'vue'
|
|
24
|
+
import SocleField from './SocleField.vue'
|
|
25
|
+
|
|
26
|
+
defineOptions({ inheritAttrs: false })
|
|
27
|
+
|
|
28
|
+
const props = withDefaults(
|
|
29
|
+
defineProps<{
|
|
30
|
+
label?: string
|
|
31
|
+
type?: 'text' | 'email' | 'password' | 'search' | 'tel' | 'url' | 'number'
|
|
32
|
+
placeholder?: string
|
|
33
|
+
name?: string
|
|
34
|
+
/** Texte d'aide sous le champ (remplacé par `error` si présent). */
|
|
35
|
+
hint?: string
|
|
36
|
+
/** Message d'erreur : met le champ en erreur. */
|
|
37
|
+
error?: string
|
|
38
|
+
disabled?: boolean
|
|
39
|
+
readonly?: boolean
|
|
40
|
+
required?: boolean
|
|
41
|
+
}>(),
|
|
42
|
+
{ type: 'text' },
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
// .trim et .number sont appliqués par Vue lui-même à l'émission de `update:modelValue` :
|
|
46
|
+
// le composant ne gère que .lazy (inexistant nativement pour un composant).
|
|
47
|
+
const [model, modifiers] = defineModel<string | number | null, 'trim' | 'number' | 'lazy'>({
|
|
48
|
+
default: null,
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
defineSlots<{
|
|
52
|
+
prefix?(): unknown
|
|
53
|
+
suffix?(): unknown
|
|
54
|
+
}>()
|
|
55
|
+
|
|
56
|
+
/* ---------- Identifiants ---------- */
|
|
57
|
+
const uid = useId()
|
|
58
|
+
// Un `id` passé en attribut est repris (utile pour un <label for> externe).
|
|
59
|
+
const attrs = useAttrs()
|
|
60
|
+
const inputId = computed(() => (attrs.id as string | undefined) ?? `${uid}-input`)
|
|
61
|
+
|
|
62
|
+
/* ---------- Valeur affichée ----------
|
|
63
|
+
Pas de `:value` : il réécrirait le champ à chaque différence et casserait la frappe
|
|
64
|
+
avec .trim / .number (« 1.0 » → « 1 », espace final supprimé). Comme le v-model natif,
|
|
65
|
+
on n'écrit dans le DOM que si le texte saisi ne correspond pas déjà à la valeur du modèle. */
|
|
66
|
+
const inputEl = ref<HTMLInputElement | null>(null)
|
|
67
|
+
let composing = false
|
|
68
|
+
|
|
69
|
+
/** Même conversion que Vue pour .number (parseFloat, texte conservé si non numérique). */
|
|
70
|
+
const looseToNumber = (s: string): string | number => {
|
|
71
|
+
const n = parseFloat(s)
|
|
72
|
+
return Number.isNaN(n) ? s : n
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function syncDom() {
|
|
76
|
+
const el = inputEl.value
|
|
77
|
+
if (!el || composing) return
|
|
78
|
+
const v = model.value
|
|
79
|
+
const next = v == null ? '' : String(v)
|
|
80
|
+
if (el.value === next) return
|
|
81
|
+
const root = el.getRootNode() as Document | ShadowRoot
|
|
82
|
+
if (root.activeElement === el) {
|
|
83
|
+
if (modifiers.trim && el.value.trim() === next) return
|
|
84
|
+
if ((modifiers.number || props.type === 'number') && looseToNumber(el.value) === v) return
|
|
85
|
+
}
|
|
86
|
+
el.value = next
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
onMounted(syncDom)
|
|
90
|
+
watch(model, syncDom, { flush: 'post' })
|
|
91
|
+
|
|
92
|
+
/** Lecture "lazy" : le modèle n'est mis à jour qu'au `change`, sinon à chaque frappe.
|
|
93
|
+
* Pendant une composition IME (japonais, chinois, accents morts…) on attend la fin. */
|
|
94
|
+
const emitValue = (el: HTMLInputElement) => {
|
|
95
|
+
model.value = el.value
|
|
96
|
+
}
|
|
97
|
+
const onInput = (e: Event) => {
|
|
98
|
+
if (!modifiers.lazy && !composing) emitValue(e.target as HTMLInputElement)
|
|
99
|
+
}
|
|
100
|
+
const onChange = (e: Event) => {
|
|
101
|
+
if (modifiers.lazy) emitValue(e.target as HTMLInputElement)
|
|
102
|
+
}
|
|
103
|
+
const onCompositionStart = () => {
|
|
104
|
+
composing = true
|
|
105
|
+
}
|
|
106
|
+
const onCompositionEnd = (e: CompositionEvent) => {
|
|
107
|
+
composing = false
|
|
108
|
+
if (!modifiers.lazy) emitValue(e.target as HTMLInputElement)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/* ---------- Classes (exclusives : pas de conflit d'ordre CSS Tailwind) ---------- */
|
|
112
|
+
/* Cadre, focus, erreur et survol : utilitaire socle-field-box (tailwind.css),
|
|
113
|
+
qui lit aria-invalid et :disabled sur l'<input>. */
|
|
114
|
+
const boxClass = computed(() => [
|
|
115
|
+
props.readonly ? 'bg-socle-surface-muted' : 'bg-socle-surface',
|
|
116
|
+
props.disabled ? 'cursor-not-allowed opacity-50' : '',
|
|
117
|
+
])
|
|
118
|
+
</script>
|
|
119
|
+
|
|
120
|
+
<template>
|
|
121
|
+
<SocleField
|
|
122
|
+
v-slot="{ describedBy }"
|
|
123
|
+
:control-id="inputId"
|
|
124
|
+
:label="label"
|
|
125
|
+
:required="required"
|
|
126
|
+
:hint="hint"
|
|
127
|
+
:error="error"
|
|
128
|
+
:class="$attrs.class"
|
|
129
|
+
:style="$attrs.style as any"
|
|
130
|
+
>
|
|
131
|
+
<!-- Le conteneur porte la bordure : le focus d'un seul <input> l'éclaire en entier -->
|
|
132
|
+
<div
|
|
133
|
+
class="socle-field-box flex items-center gap-2"
|
|
134
|
+
:class="boxClass"
|
|
135
|
+
>
|
|
136
|
+
<span v-if="$slots.prefix" class="flex shrink-0 items-center text-socle-text-subtle">
|
|
137
|
+
<slot name="prefix" />
|
|
138
|
+
</span>
|
|
139
|
+
|
|
140
|
+
<input
|
|
141
|
+
v-bind="{ ...$attrs, class: undefined, style: undefined }"
|
|
142
|
+
ref="inputEl"
|
|
143
|
+
:id="inputId"
|
|
144
|
+
:type="type"
|
|
145
|
+
:name="name"
|
|
146
|
+
:placeholder="placeholder"
|
|
147
|
+
:disabled="disabled"
|
|
148
|
+
:readonly="readonly"
|
|
149
|
+
:required="required"
|
|
150
|
+
:aria-invalid="error ? true : undefined"
|
|
151
|
+
:aria-describedby="describedBy"
|
|
152
|
+
class="min-w-0 flex-1 bg-transparent py-2 outline-none placeholder:text-socle-placeholder disabled:cursor-not-allowed"
|
|
153
|
+
@input="onInput"
|
|
154
|
+
@change="onChange"
|
|
155
|
+
@compositionstart="onCompositionStart"
|
|
156
|
+
@compositionend="onCompositionEnd"
|
|
157
|
+
/>
|
|
158
|
+
|
|
159
|
+
<span v-if="$slots.suffix" class="flex shrink-0 items-center text-socle-text-subtle">
|
|
160
|
+
<slot name="suffix" />
|
|
161
|
+
</span>
|
|
162
|
+
</div>
|
|
163
|
+
</SocleField>
|
|
164
|
+
</template>
|
package/SoclePopover.vue
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
SoclePopover.vue
|
|
3
|
+
Panneau flottant ancré à un bouton : menu d'actions, aide contextuelle, petit formulaire…
|
|
4
|
+
Prérequis : Vue >= 3.5 (useId), Tailwind CSS v4.
|
|
5
|
+
|
|
6
|
+
<SoclePopover placement="bottom" align="start" aria-label="Options d'affichage">
|
|
7
|
+
<template #trigger="{ props }">
|
|
8
|
+
<button v-bind="props" type="button">Options</button>
|
|
9
|
+
</template>
|
|
10
|
+
<p>Contenu du panneau…</p>
|
|
11
|
+
</SoclePopover>
|
|
12
|
+
|
|
13
|
+
- API Popover (`popovertarget`) : ouverture / fermeture au clic, top layer, fermeture par Échap
|
|
14
|
+
et par clic extérieur (light dismiss), retour du focus au bouton, aria-expanded implicite,
|
|
15
|
+
ordre de tabulation bouton → panneau.
|
|
16
|
+
- CSS Anchor Positioning : `position-area`, `position-try-fallbacks` (retournement automatique
|
|
17
|
+
quand la place manque), `position-visibility` (masqué si le bouton sort de la vue).
|
|
18
|
+
- Requêtes de conteneur ancré (`container-type: anchored`) : la flèche suit le retournement.
|
|
19
|
+
- `@starting-style` + `transition-behavior: allow-discrete` : animation d'entrée ET de sortie.
|
|
20
|
+
|
|
21
|
+
- Slot `trigger` : reçoit `props` à lier sur le bouton déclencheur (v-bind), et `open`.
|
|
22
|
+
- v-model : ouvert / fermé (facultatif : le popover fonctionne sans).
|
|
23
|
+
- placement : côté du bouton où s'ouvre le panneau ; align : alignement sur l'autre axe.
|
|
24
|
+
- class/style : appliqués au panneau (ex. largeur `class="w-72"`).
|
|
25
|
+
|
|
26
|
+
Tout le comportement est natif, sans JavaScript de positionnement :
|
|
27
|
+
-->
|
|
28
|
+
<script lang="ts">
|
|
29
|
+
/*
|
|
30
|
+
Requêtes de conteneur ancré : la flèche suit le retournement du panneau.
|
|
31
|
+
Elles ne sont pas dans socle.css car les minifieurs CSS (Lightning CSS, utilisé par Vite)
|
|
32
|
+
ne connaissent pas encore `anchored(…)` et feraient échouer le build de l'application.
|
|
33
|
+
Injectées une seule fois, et seulement si le navigateur les comprend
|
|
34
|
+
(sinon socle.css masque la flèche).
|
|
35
|
+
*/
|
|
36
|
+
const anchoredCss = `
|
|
37
|
+
@container anchored(fallback: flip-block) { .socle-popover-arrow { --_fy: 1; } }
|
|
38
|
+
@container anchored(fallback: flip-inline) { .socle-popover-arrow { --_fx: 1; } }
|
|
39
|
+
@container anchored(fallback: flip-block flip-inline) { .socle-popover-arrow { --_fx: 1; --_fy: 1; } }
|
|
40
|
+
@container anchored(fallback: flip-inline flip-block) { .socle-popover-arrow { --_fx: 1; --_fy: 1; } }
|
|
41
|
+
`
|
|
42
|
+
|
|
43
|
+
let anchoredInstalled = false
|
|
44
|
+
|
|
45
|
+
function installAnchoredCss() {
|
|
46
|
+
if (anchoredInstalled || typeof document === 'undefined') return
|
|
47
|
+
anchoredInstalled = true
|
|
48
|
+
if (!CSS.supports('container-type', 'anchored')) return
|
|
49
|
+
const sheet = new CSSStyleSheet()
|
|
50
|
+
sheet.replaceSync(anchoredCss)
|
|
51
|
+
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet]
|
|
52
|
+
}
|
|
53
|
+
</script>
|
|
54
|
+
|
|
55
|
+
<script setup lang="ts">
|
|
56
|
+
import { computed, onMounted, ref, useId, watch } from 'vue'
|
|
57
|
+
import './socle.css'
|
|
58
|
+
|
|
59
|
+
export type PopoverPlacement = 'top' | 'bottom' | 'left' | 'right'
|
|
60
|
+
export type PopoverAlign = 'start' | 'center' | 'end'
|
|
61
|
+
|
|
62
|
+
defineOptions({ inheritAttrs: false })
|
|
63
|
+
|
|
64
|
+
const props = withDefaults(
|
|
65
|
+
defineProps<{
|
|
66
|
+
/** Côté du bouton où s'ouvre le panneau (retourné automatiquement si la place manque). */
|
|
67
|
+
placement?: PopoverPlacement
|
|
68
|
+
/** Alignement sur l'autre axe : start = bord gauche (ou haut) du bouton, end = bord droit (ou bas). */
|
|
69
|
+
align?: PopoverAlign
|
|
70
|
+
/** Petite flèche pointant vers le bouton. */
|
|
71
|
+
arrow?: boolean
|
|
72
|
+
/** Persistant : ne se ferme ni par clic extérieur, ni à l'ouverture d'un autre popover.
|
|
73
|
+
* Échap, le bouton ou le v-model le ferment toujours. */
|
|
74
|
+
persistent?: boolean
|
|
75
|
+
}>(),
|
|
76
|
+
{ placement: 'bottom', align: 'center', arrow: true, persistent: false },
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
const open = defineModel<boolean>({ default: false })
|
|
80
|
+
|
|
81
|
+
defineSlots<{
|
|
82
|
+
trigger?(props: {
|
|
83
|
+
props: { popovertarget: string; style: Record<string, string> }
|
|
84
|
+
open: boolean
|
|
85
|
+
}): unknown
|
|
86
|
+
default?(): unknown
|
|
87
|
+
}>()
|
|
88
|
+
|
|
89
|
+
/* ---------- Identifiants ---------- */
|
|
90
|
+
const uid = useId()
|
|
91
|
+
const popoverId = `${uid}-popover`
|
|
92
|
+
// Un nom d'ancre unique par instance (dashed-ident) pour pouvoir en afficher plusieurs.
|
|
93
|
+
const anchorName = `--popover-${uid.replace(/[^a-zA-Z0-9_-]/g, '')}`
|
|
94
|
+
|
|
95
|
+
/** À lier sur le bouton : le navigateur gère seul le clic, Échap, le focus et aria-expanded. */
|
|
96
|
+
const triggerProps = computed(() => ({
|
|
97
|
+
popovertarget: popoverId,
|
|
98
|
+
style: { 'anchor-name': anchorName },
|
|
99
|
+
}))
|
|
100
|
+
|
|
101
|
+
/* ---------- Ouverture / fermeture ---------- */
|
|
102
|
+
const popover = ref<HTMLElement | null>(null)
|
|
103
|
+
|
|
104
|
+
const isOpen = () => popover.value?.matches(':popover-open') ?? false
|
|
105
|
+
|
|
106
|
+
/** Bouton déclencheur : passé comme `source`, il garde le lien invocateur
|
|
107
|
+
* (retour du focus, popovers imbriqués) même lors d'une ouverture par v-model. */
|
|
108
|
+
const triggerEl = () => document.querySelector<HTMLElement>(`[popovertarget="${CSS.escape(popoverId)}"]`)
|
|
109
|
+
|
|
110
|
+
function show() {
|
|
111
|
+
if (!popover.value || isOpen()) return
|
|
112
|
+
// `source` est ignoré par les navigateurs qui ne le connaissent pas.
|
|
113
|
+
;(popover.value.showPopover as (options?: { source?: HTMLElement | null }) => void)({ source: triggerEl() })
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function hide() {
|
|
117
|
+
if (isOpen()) popover.value?.hidePopover()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function toggle() {
|
|
121
|
+
isOpen() ? hide() : show()
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Ouverture / fermeture native (bouton, Échap, clic extérieur) : on resynchronise le v-model. */
|
|
125
|
+
function onToggle(e: Event) {
|
|
126
|
+
open.value = (e as ToggleEvent).newState === 'open'
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** En mode persistant (popover="manual"), le navigateur ne gère pas Échap : on le fait,
|
|
130
|
+
* et on rend le focus au bouton comme le ferait un popover « auto ». */
|
|
131
|
+
function onEscape(e: KeyboardEvent) {
|
|
132
|
+
if (!props.persistent || e.defaultPrevented) return
|
|
133
|
+
e.preventDefault()
|
|
134
|
+
const focusInside = popover.value?.contains(document.activeElement)
|
|
135
|
+
hide()
|
|
136
|
+
if (focusInside) triggerEl()?.focus()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// flush: 'post' → le popover est bien dans le DOM quand on réagit.
|
|
140
|
+
watch(open, (v) => (v ? show() : hide()), { flush: 'post' })
|
|
141
|
+
|
|
142
|
+
onMounted(() => {
|
|
143
|
+
installAnchoredCss()
|
|
144
|
+
if (open.value) show()
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
defineExpose({ show, hide, toggle })
|
|
148
|
+
</script>
|
|
149
|
+
|
|
150
|
+
<template>
|
|
151
|
+
<slot name="trigger" :props="triggerProps" :open="open" />
|
|
152
|
+
|
|
153
|
+
<div
|
|
154
|
+
:id="popoverId"
|
|
155
|
+
ref="popover"
|
|
156
|
+
:popover="persistent ? 'manual' : 'auto'"
|
|
157
|
+
:data-placement="placement"
|
|
158
|
+
:data-align="align"
|
|
159
|
+
:data-arrow="arrow || undefined"
|
|
160
|
+
class="socle-popover rounded-socle border border-socle-border-subtle bg-socle-surface text-sm text-socle-text shadow-lg"
|
|
161
|
+
:style="{ '--socle-anchor': anchorName }"
|
|
162
|
+
v-bind="$attrs"
|
|
163
|
+
@toggle="onToggle"
|
|
164
|
+
@keydown.esc="onEscape"
|
|
165
|
+
>
|
|
166
|
+
<span v-if="arrow" class="socle-popover-arrow" aria-hidden="true" />
|
|
167
|
+
<div class="socle-popover-body">
|
|
168
|
+
<slot />
|
|
169
|
+
</div>
|
|
170
|
+
</div>
|
|
171
|
+
</template>
|