@splendidlabz/svelte 4.16.0 → 4.17.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.
@@ -1,6 +1,7 @@
1
1
  <script>
2
2
  import { randomString } from '@splendidlabz/utils/dom'
3
3
  import { useActions } from '../../actions/use-actions'
4
+ import FieldMeta from './FieldMeta.svelte'
4
5
 
5
6
  let {
6
7
  value = $bindable(''),
@@ -15,6 +16,7 @@
15
16
  id = randomString(8),
16
17
  name,
17
18
  label = '',
19
+ labelVisible = true,
18
20
  hint = null,
19
21
  error = null,
20
22
  disabled = null,
@@ -36,21 +38,8 @@
36
38
  const errorId = $derived(id + '-error')
37
39
  const describedBy = $derived(`${hintId} ${errorId}`.trim() || null)
38
40
 
39
- // ========================================
40
- // Extra Features
41
- // ========================================
42
- // Think about auto-growing textarea
43
- let dummyRef = $state(null)
44
- function autoGrow(event) {
45
- if (autogrow) {
46
- dummyRef.textContent = event.target.value
47
- const last = event.target.value.slice(-1)
48
- if (last === '\n') dummyRef.textContent += '&nbsp;'
49
- }
50
-
51
- // Fire oninput event too... if any
52
- if (oninput) oninput(event)
53
- }
41
+ // Mirrors the text, and fills a trailing newline so its line counts
42
+ const replicaText = $derived(value + (value.endsWith('\n') ? '\u00a0' : ''))
54
43
  </script>
55
44
 
56
45
  <!-- Snippets -->
@@ -63,12 +52,12 @@
63
52
  class="input {fieldClass}"
64
53
  aria-describedby={describedBy}
65
54
  aria-invalid={error ? 'true' : null}
55
+ aria-label={!labelVisible && label ? label : null}
66
56
  {disabled}
67
- oninput={autoGrow}
57
+ {oninput}
68
58
  {readonly}
69
59
  bind:value
70
- use:useActions={actions}
71
- ></textarea>
60
+ use:useActions={actions}></textarea>
72
61
  {/snippet}
73
62
 
74
63
  <div
@@ -78,21 +67,16 @@
78
67
  class:readonly
79
68
  class:textarea-autogrow={autogrow}
80
69
  >
81
- {#if label}
82
- <label class="label input-meta {labelClass}" for={id}>{label}</label>
83
- {/if}
84
- {#if hint}
85
- <div id={hintId} class="hint input-meta {hintClass}">{hint}</div>
86
- {/if}
87
- <output
88
- id={errorId}
89
- for={id}
90
- class="error input-meta {errorClass}"
91
- class:sr-only={!error}
92
- role="alert"
93
- >
94
- {#if error}{error}{/if}
95
- </output>
70
+ <FieldMeta
71
+ {id}
72
+ {error}
73
+ {errorClass}
74
+ {hint}
75
+ {hintClass}
76
+ {label}
77
+ {labelClass}
78
+ {labelVisible}
79
+ />
96
80
 
97
81
  {#if before || after}
98
82
  <div class="input-group {inputGroupClass}">
@@ -111,7 +95,11 @@
111
95
  {:else}
112
96
  <div class="stack">
113
97
  <!-- Autogrow textarea cannot be used together with input-group cos it makes the CSS very complex -->
114
- <div bind:this={dummyRef} class="replica-field"></div>
98
+ {#if autogrow}
99
+ <div class="replica-field field {fieldClass}" aria-hidden="true">
100
+ {replicaText}
101
+ </div>
102
+ {/if}
115
103
  {@render textarea()}
116
104
  </div>
117
105
  {/if}
@@ -8,8 +8,8 @@ export { default as Hidden } from './Hidden.svelte'
8
8
  export { default as HiddenInput } from './HiddenInput.svelte'
9
9
  export { default as Honeypot } from './Honeypot.svelte'
10
10
  export { default as Radio } from './Radio.svelte'
11
+ export { default as NativeSelect } from './NativeSelect.svelte'
11
12
  export { default as Radios } from './Radios.svelte'
12
- export { default as Select } from './Select.svelte'
13
13
  export { default as Switch } from './Switch.svelte'
14
14
  export { default as Textarea } from './Textarea.svelte'
15
15
  export { default as TextInput } from './TextInput.svelte'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@splendidlabz/svelte",
3
3
  "type": "module",
4
- "version": "4.16.0",
4
+ "version": "4.17.0",
5
5
  "license": "Elastic-2.0",
6
6
  "sideEffects": false,
7
7
  "homepage": "https://splendidlabz.com/docs/svelte",
@@ -37,7 +37,7 @@
37
37
  "@splendidlabz/utils": "*"
38
38
  },
39
39
  "devDependencies": {
40
- "@splendidlabz/eslint-config": "2.2.0",
40
+ "@splendidlabz/eslint-config": "2.3.1",
41
41
  "@sveltejs/vite-plugin-svelte": "^7.0.0",
42
42
  "happy-dom": "^20.9.0",
43
43
  "svelte": "^5.55.4",
@@ -1,40 +0,0 @@
1
- <script>
2
- import { autocompleteInput } from './autocomplete.svelte.js'
3
- import ComboboxInput from './ComboboxInput.svelte'
4
-
5
- /**
6
- * @import { ComponentProps } from 'svelte'
7
- */
8
-
9
- /**
10
- * @typedef {object} AutocompleteInputOwnProps
11
- * @property {(query: string) => Promise<any[]>} [search] Replaces local filtering
12
- * @property {number} [debounceMs] Wait before searching
13
- * @property {(item: any) => void} [onselect] Receives the item, or the option element
14
- */
15
-
16
- /** @type {ComponentProps<typeof ComboboxInput> & AutocompleteInputOwnProps} */
17
- let {
18
- value = $bindable(''),
19
- ref = $bindable(null),
20
- search,
21
- debounceMs,
22
- loop = true,
23
- onselect,
24
- ...rest
25
- } = $props()
26
- </script>
27
-
28
- <ComboboxInput
29
- {...rest}
30
- actions={[
31
- {
32
- fn: autocompleteInput,
33
- props: { options: { search, debounceMs, onselect } },
34
- },
35
- ]}
36
- aria-autocomplete="list"
37
- {loop}
38
- bind:value
39
- bind:ref
40
- />
@@ -1,50 +0,0 @@
1
- <script>
2
- import { init } from '../../../utils'
3
- import TextInput from '../TextInput.svelte'
4
- import { comboboxInput } from './combobox.svelte.js'
5
-
6
- /**
7
- * @import { ComponentProps } from 'svelte'
8
- */
9
-
10
- /**
11
- * @typedef {object} ComboboxInputOwnProps
12
- * @property {string} target Id of the options
13
- * @property {boolean} [loop] Wrap past either end
14
- * @property {(option: HTMLElement) => void} [onselect] Receives the selected option
15
- *
16
- */
17
-
18
- /** @type {ComponentProps<typeof TextInput> & ComboboxInputOwnProps} */
19
- let {
20
- value = $bindable(''),
21
- ref = $bindable(null),
22
- target,
23
- loop,
24
- onselect,
25
- actions = [],
26
- ...rest
27
- } = $props()
28
-
29
- init(() => {
30
- if (!target) throw new Error('ComboboxInput requires a target')
31
- })
32
- </script>
33
-
34
- <TextInput
35
- {...rest}
36
- actions={[
37
- {
38
- fn: comboboxInput,
39
- props: { options: { loop, onselect } },
40
- },
41
- ...actions,
42
- ]}
43
- aria-controls="{target}-listbox"
44
- aria-expanded="false"
45
- autocomplete="off"
46
- role="combobox"
47
- {target}
48
- bind:value
49
- bind:ref
50
- />
@@ -1,83 +0,0 @@
1
- /**
2
- * @file Matches option texts against a typed query.
3
- *
4
- * match strips diacritics before matching, so "cafe" matches "Café".
5
- */
6
- import { match } from './match.js'
7
-
8
- /**
9
- * getMatches
10
- * Finds the texts a query matches, and where each match sits.
11
- *
12
- * @typedef {[number, number]} Range Start and end index of a matched part.
13
- * @typedef {{ index: number, ranges: Range[] }} Match `index` points into the texts passed in.
14
- *
15
- * @param {object} opts
16
- * @param {string[]} opts.texts Texts to search
17
- * @param {string} opts.query Typed query
18
- * @returns {Match[]} Word-start matches first, then inside-word matches.
19
- */
20
- export function getMatches({ texts, query }) {
21
- const wordStartIndexes = getMatchingIndexes({
22
- texts,
23
- query,
24
- insideWords: false,
25
- })
26
- const insideWordIndexes = getMatchingIndexes({
27
- texts,
28
- query,
29
- insideWords: true,
30
- })
31
- const indexes = new Set([...wordStartIndexes, ...insideWordIndexes])
32
-
33
- return Array.from(indexes, index => ({
34
- index,
35
- ranges: match({ text: texts[index], query, insideWords: true }),
36
- }))
37
- }
38
-
39
- /**
40
- * hasPerfectMatch
41
- * Checks whether the query equals one of the texts, ignoring case and diacritics.
42
- *
43
- * @param {object} opts
44
- * @param {string[]} opts.texts Texts to search
45
- * @param {string} opts.query Typed query
46
- * @returns {boolean}
47
- */
48
- export function hasPerfectMatch({ texts, query }) {
49
- return texts.some(text => isPerfectMatch({ text, query }))
50
- }
51
-
52
- /**
53
- * getMatchingIndexes
54
- * Gets the indexes of the texts a query matches.
55
- *
56
- * @param {object} opts
57
- * @param {string[]} opts.texts Texts to search
58
- * @param {string} opts.query Typed query
59
- * @param {boolean} opts.insideWords Match inside words too
60
- * @returns {number[]}
61
- */
62
- function getMatchingIndexes({ texts, query, insideWords }) {
63
- return texts.flatMap((text, index) => {
64
- const ranges = match({ text, query, insideWords })
65
- return ranges.length ? [index] : []
66
- })
67
- }
68
-
69
- /**
70
- * isPerfectMatch
71
- * Checks whether the query covers the whole text.
72
- *
73
- * @param {object} opts
74
- * @param {string} opts.text Text to compare
75
- * @param {string} opts.query Typed query
76
- */
77
- function isPerfectMatch({ text, query }) {
78
- if (text.length !== query.length) return false
79
-
80
- const ranges = match({ text, query, insideWords: true })
81
- if (!ranges.length) return false
82
- return ranges[0][0] === 0 && ranges[ranges.length - 1][1] === text.length
83
- }