@ternarytech-io/gears-ui 0.3.3 → 0.3.4

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/README.md CHANGED
@@ -102,7 +102,7 @@ add a page, add an entry to `preview/registry.ts` and a folder of examples.
102
102
  | Group | Components |
103
103
  | --- | --- |
104
104
  | Elements | `TButton`, `TBadge`, `TAvatar`, `TAlert`, `TCard`, `TIcon`, `TTooltip`, `TEmpty`, `TSpinner`, `TSkeleton`, `TProgress`, `TSeparator` |
105
- | Forms | `TFormField`, `TInput`, `TInputNumber`, `TTextarea`, `TSelect`, `TSelectMenu`, `TInputTags`, `TCheckbox`, `TSwitch`, `TRadioGroup` (list, cards or tiles with a picture), `TFieldGroup`, `TFileUpload`, `TImageUpload` (one image, checked for shape and size), `TIconPicker` (any Tabler icon, by name, with a search; the list is generated by `npm run icons`), `TRating` (stars, hearts or circles; no library), `TCodeEditor` |
105
+ | Forms | `TFormField`, `TInput`, `TInputNumber`, `TTextarea`, `TSelect`, `TSelectMenu`, `TInputTags`, `TCheckbox`, `TSwitch`, `TRadioGroup` (list, cards or tiles with a picture), `TFieldGroup`, `TFileUpload`, `TImageUpload` (one image, checked for shape and size), `TIconPicker` (any Tabler icon, by name, with a search; the list is generated by `npm run icons`), `TRating` (stars, hearts or circles; no library), `TListBox` (always shown; one or several; a search), `TCheckboxTree` (parents check their children; partly checked; folding), `TCodeEditor` |
106
106
  | Theme | `TAccentPicker`, `TFontPicker`, `TRadiusPicker`, `TThemeBasePicker` (Tabler's theme settings as pickers), `TNavPositionPicker`, `TDensityPicker`, `TContainerPicker` (layout), `TThemeScope` (applies a `ThemeSettings`' look and density to what's inside it); `themeAccents`, `themeFonts`, `themeBases`, `themeRadii`, `themeNavs`, `themeDensities`, `themeContainers` |
107
107
  | Overlays | `TModal`, `TSlideover`, `TDropdown`, `TToaster` (with `useToast()`) |
108
108
  | Navigation | `TTabs`, `TPagination`, `TSteps`, `TNav`, `TPanelList`, `TCommandPalette`, `TSearchButton` |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ternarytech-io/gears-ui",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "type": "module",
5
5
  "description": "Gears UI: Vue 3 components on Tabler, used by the Gears Studio and the applications it builds.",
6
6
  "license": "MPL-2.0",
@@ -0,0 +1,82 @@
1
+ <script setup lang="ts" generic="T">
2
+ // Checkboxes in a tree: a parent is checked when all its children are, partly checked (dash) when
3
+ // some are, and checking it checks them all. Parents fold and open with their arrow (`collapsible`,
4
+ // all open at first unless `collapsed`). v-model is the checked leaves' values.
5
+ import { computed, shallowRef } from 'vue'
6
+ import TIcon from './TIcon.vue'
7
+ import type { TreeItem } from '../types'
8
+
9
+ const props = withDefaults(defineProps<{
10
+ items: TreeItem<T>[]
11
+ collapsible?: boolean
12
+ collapsed?: boolean
13
+ disabled?: boolean
14
+ // The tree's accessible name.
15
+ label?: string
16
+ }>(), { collapsible: true, collapsed: false })
17
+
18
+ const model = defineModel<T[]>({ default: () => [] })
19
+ // The parents folded, by value.
20
+ const folded = shallowRef(new Set<T>())
21
+ if (props.collapsed) {
22
+ const walk = (items: TreeItem<T>[]) => items.forEach(i => { if (i.children?.length) { folded.value.add(i.value); walk(i.children) } })
23
+ walk(props.items)
24
+ }
25
+
26
+ const checked = computed(() => new Set(model.value))
27
+ const leaves = (item: TreeItem<T>): T[] => (item.children?.length ? item.children.flatMap(leaves) : [item.value])
28
+ function state(item: TreeItem<T>): 'on' | 'off' | 'some' {
29
+ const all = leaves(item)
30
+ const on = all.filter(v => checked.value.has(v)).length
31
+ return on === 0 ? 'off' : on === all.length ? 'on' : 'some'
32
+ }
33
+ function toggle(item: TreeItem<T>) {
34
+ if (props.disabled || item.disabled) return
35
+ const all = leaves(item)
36
+ const next = new Set(model.value)
37
+ if (state(item) === 'on') all.forEach(v => next.delete(v))
38
+ else all.forEach(v => next.add(v))
39
+ model.value = [...next]
40
+ }
41
+ function fold(item: TreeItem<T>) {
42
+ const next = new Set(folded.value)
43
+ if (next.has(item.value)) next.delete(item.value)
44
+ else next.add(item.value)
45
+ folded.value = next
46
+ }
47
+ // Rows to show, with their depth: what's under a folded parent is left out.
48
+ const rows = computed(() => {
49
+ const out: { item: TreeItem<T>; depth: number }[] = []
50
+ const walk = (items: TreeItem<T>[], depth: number) => items.forEach(item => {
51
+ out.push({ item, depth })
52
+ if (item.children?.length && !(props.collapsible && folded.value.has(item.value))) walk(item.children, depth + 1)
53
+ })
54
+ walk(props.items, 0)
55
+ return out
56
+ })
57
+ </script>
58
+
59
+ <template>
60
+ <div class="t-checkbox-tree" role="group" :aria-label="label">
61
+ <div v-for="{ item, depth } in rows" :key="String(item.value)" class="t-checkbox-tree-row" :style="{ paddingInlineStart: `${depth * 1.25}rem` }">
62
+ <button v-if="collapsible && item.children?.length" type="button" class="t-checkbox-tree-fold" :aria-expanded="!folded.has(item.value)"
63
+ :aria-label="`${folded.has(item.value) ? 'Open' : 'Fold'} ${item.label}`" @click="fold(item)">
64
+ <TIcon :name="folded.has(item.value) ? 'chevron-right' : 'chevron-down'" />
65
+ </button>
66
+ <span v-else-if="collapsible" class="t-checkbox-tree-fold" aria-hidden="true" />
67
+ <label class="form-check mb-0">
68
+ <input type="checkbox" class="form-check-input" :checked="state(item) === 'on'" :indeterminate.prop="state(item) === 'some'"
69
+ :aria-checked="state(item) === 'some' ? 'mixed' : state(item) === 'on'" :disabled="disabled || item.disabled" @change="toggle(item)">
70
+ <span class="form-check-label">{{ item.label }}</span>
71
+ </label>
72
+ </div>
73
+ </div>
74
+ </template>
75
+
76
+ <style scoped>
77
+ .t-checkbox-tree { display: flex; flex-direction: column; gap: 0.25rem; }
78
+ .t-checkbox-tree-row { display: flex; align-items: center; gap: 0.25rem; }
79
+ .t-checkbox-tree-row .form-check { min-height: 0; }
80
+ .t-checkbox-tree-fold { display: grid; place-items: center; flex-shrink: 0; width: 1.25rem; height: 1.25rem; padding: 0; border: 0; background: none; color: var(--tblr-secondary); }
81
+ button.t-checkbox-tree-fold:hover { color: var(--tblr-body-color); }
82
+ </style>
@@ -0,0 +1,111 @@
1
+ <script setup lang="ts" generic="T">
2
+ // A list of choices that's always shown (not a dropdown), to choose one or, with `multiple`,
3
+ // several; `searchable` adds a search above it. It's a listbox for assistive technology: the arrow
4
+ // keys, Home and End move through it, Space or Enter chooses. v-model is the chosen value (null for
5
+ // none), or with `multiple` the chosen values.
6
+ import { computed, ref, useId } from 'vue'
7
+ import TIcon from './TIcon.vue'
8
+ import { useField } from '../composables/useField'
9
+ import type { SelectItem } from '../types'
10
+
11
+ const props = withDefaults(defineProps<{
12
+ items: (T | SelectItem<T>)[]
13
+ multiple?: boolean
14
+ searchable?: boolean
15
+ searchPlaceholder?: string
16
+ // The list's tallest (CSS); it scrolls past that. `none` lets it fill the box it's in (give the
17
+ // TListBox a height, or put it in a flex column).
18
+ height?: string
19
+ disabled?: boolean
20
+ invalid?: boolean
21
+ // The list's accessible name, when no label points at it.
22
+ label?: string
23
+ id?: string
24
+ }>(), { searchPlaceholder: 'Search…', height: '12rem' })
25
+
26
+ const model = defineModel<T | T[] | null | undefined>()
27
+ const field = useField(props)
28
+ const listId = useId()
29
+ const query = ref('')
30
+ const active = ref(0)
31
+ const list = ref<HTMLElement>()
32
+
33
+ const options = computed(() => props.items.map(item => (item !== null && typeof item === 'object' && 'value' in (item as object) && 'label' in (item as object)
34
+ ? item as SelectItem<T>
35
+ : { label: String(item), value: item as T })))
36
+ const shown = computed(() => {
37
+ const term = query.value.trim().toLowerCase()
38
+ return term ? options.value.filter(o => o.label.toLowerCase().includes(term) || (o.description ?? '').toLowerCase().includes(term)) : options.value
39
+ })
40
+ const chosen = (value: T) => props.multiple ? Array.isArray(model.value) && model.value.includes(value) : model.value === value
41
+
42
+ function choose(option: SelectItem<T>) {
43
+ if (props.disabled || option.disabled) return
44
+ if (!props.multiple) {
45
+ model.value = option.value
46
+ return
47
+ }
48
+ const current = Array.isArray(model.value) ? model.value : []
49
+ model.value = current.includes(option.value) ? current.filter(v => v !== option.value) : [...current, option.value]
50
+ }
51
+ function key(event: KeyboardEvent) {
52
+ const last = shown.value.length - 1
53
+ if (last < 0) return
54
+ const moves: Record<string, number> = { ArrowDown: active.value + 1, ArrowUp: active.value - 1, Home: 0, End: last }
55
+ if (event.key in moves) {
56
+ event.preventDefault()
57
+ active.value = Math.max(0, Math.min(last, moves[event.key]!))
58
+ list.value?.querySelector<HTMLElement>(`[data-index="${active.value}"]`)?.scrollIntoView({ block: 'nearest' })
59
+ } else if (event.key === ' ' || event.key === 'Enter') {
60
+ event.preventDefault()
61
+ choose(shown.value[active.value]!)
62
+ }
63
+ }
64
+ </script>
65
+
66
+ <template>
67
+ <div :class="['t-list-box', { 'is-invalid': invalid || field.invalid.value, disabled }]">
68
+ <div v-if="searchable" class="t-list-box-search">
69
+ <input v-model="query" type="search" class="form-control form-control-sm" :placeholder="searchPlaceholder" :aria-label="searchPlaceholder"
70
+ :aria-controls="listId" :disabled="disabled" @input="active = 0">
71
+ </div>
72
+ <div :id="field.id.value === listId ? listId : field.id.value" ref="list" class="t-list-box-list" role="listbox" :tabindex="disabled ? -1 : 0"
73
+ :aria-multiselectable="multiple || undefined" :aria-disabled="disabled || undefined" :aria-label="label"
74
+ :aria-activedescendant="shown.length ? `${listId}-${active}` : undefined" :aria-describedby="field.describedBy.value" :style="{ maxHeight: height }"
75
+ @keydown="key">
76
+ <div v-for="(option, index) in shown" :id="`${listId}-${index}`" :key="index" :data-index="index" role="option"
77
+ :aria-selected="chosen(option.value)" :aria-disabled="option.disabled || undefined"
78
+ :class="['t-list-box-item', { selected: chosen(option.value), active: index === active, disabled: option.disabled }]"
79
+ @click="active = index; choose(option)">
80
+ <span v-if="multiple" class="t-list-box-check" aria-hidden="true"><TIcon v-if="chosen(option.value)" name="check" /></span>
81
+ <span class="min-w-0">
82
+ <span class="d-block text-truncate">{{ option.label }}</span>
83
+ <span v-if="option.description" class="d-block small text-secondary text-truncate">{{ option.description }}</span>
84
+ </span>
85
+ <TIcon v-if="!multiple && chosen(option.value)" name="check" class="ms-auto text-primary" />
86
+ </div>
87
+ <p v-if="!shown.length" class="t-list-box-empty">{{ query ? 'Nothing matches.' : 'Nothing to choose.' }}</p>
88
+ </div>
89
+ </div>
90
+ </template>
91
+
92
+ <style scoped>
93
+ .t-list-box { display: flex; flex-direction: column; min-height: 0; border: var(--tblr-border-width) solid var(--tblr-border-color); border-radius: var(--tblr-border-radius); background: var(--tblr-bg-forms, var(--tblr-bg-surface)); overflow: hidden; }
94
+ .t-list-box.is-invalid { border-color: var(--tblr-danger); }
95
+ .t-list-box.disabled { opacity: 0.65; }
96
+ .t-list-box-search { padding: 0.375rem; border-bottom: var(--tblr-border-width) solid var(--tblr-border-color); }
97
+ .t-list-box-list { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 0.25rem; outline: none; }
98
+ .t-list-box-list:focus-visible { box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--tblr-primary) 40%, transparent); }
99
+ .t-list-box-item { display: flex; align-items: center; gap: 0.5rem; padding: 0.375rem 0.5rem; border-radius: var(--tblr-border-radius-sm); cursor: pointer; user-select: none; }
100
+ .t-list-box-item:hover { background: var(--tblr-bg-surface-secondary); }
101
+ .t-list-box-list:focus-visible .t-list-box-item.active { background: var(--tblr-bg-surface-secondary); outline: 1px solid var(--tblr-border-color); }
102
+ .t-list-box-item.selected { background: color-mix(in srgb, var(--tblr-primary) 12%, transparent); }
103
+ .t-list-box-item.disabled { opacity: 0.5; cursor: default; }
104
+ .t-list-box-check {
105
+ display: grid; place-items: center; flex-shrink: 0; width: 1rem; height: 1rem; font-size: 0.75rem;
106
+ border: var(--tblr-border-width) solid var(--tblr-border-color-dark, var(--tblr-border-color)); border-radius: var(--tblr-border-radius-sm);
107
+ }
108
+ .t-list-box-check :deep(.icon) { width: auto; height: auto; font-size: 0.75rem; line-height: 1; vertical-align: 0; }
109
+ .selected > .t-list-box-check { background: var(--tblr-primary); border-color: var(--tblr-primary); color: var(--tblr-primary-fg, #fff); }
110
+ .t-list-box-empty { margin: 0.5rem; color: var(--tblr-secondary); font-size: 0.8125rem; }
111
+ </style>
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ import TBadge from './components/TBadge.vue'
14
14
  import TButton from './components/TButton.vue'
15
15
  import TCard from './components/TCard.vue'
16
16
  import TCheckbox from './components/TCheckbox.vue'
17
+ import TCheckboxTree from './components/TCheckboxTree.vue'
17
18
  import TColorModeButton from './components/TColorModeButton.vue'
18
19
  import TCommandPalette from './components/TCommandPalette.vue'
19
20
  import TContainerPicker from './components/TContainerPicker.vue'
@@ -34,6 +35,7 @@ import TImageUpload from './components/TImageUpload.vue'
34
35
  import TInput from './components/TInput.vue'
35
36
  import TInputNumber from './components/TInputNumber.vue'
36
37
  import TInputTags from './components/TInputTags.vue'
38
+ import TListBox from './components/TListBox.vue'
37
39
  import TLogView from './components/TLogView.vue'
38
40
  import TModal from './components/TModal.vue'
39
41
  import TNav from './components/TNav.vue'
@@ -81,6 +83,7 @@ export {
81
83
  TButton,
82
84
  TCard,
83
85
  TCheckbox,
86
+ TCheckboxTree,
84
87
  TColorModeButton,
85
88
  TCommandPalette,
86
89
  TContainerPicker,
@@ -101,6 +104,7 @@ export {
101
104
  TInput,
102
105
  TInputNumber,
103
106
  TInputTags,
107
+ TListBox,
104
108
  TLogView,
105
109
  TModal,
106
110
  TNav,
@@ -147,9 +151,9 @@ export { useToast, type Toast } from './composables/useToast'
147
151
 
148
152
  const components = {
149
153
  TAccentPicker, TAdvancedTable, TAlert, TAppLayout, TAuthLayout, TAvatar, TBadge, TButton, TCard,
150
- TCheckbox, TColorModeButton, TCommandPalette, TContainerPicker, TCopyButton, TDataGrid, TDataGridItem, TDensityPicker,
154
+ TCheckbox, TCheckboxTree, TColorModeButton, TCommandPalette, TContainerPicker, TCopyButton, TDataGrid, TDataGridItem, TDensityPicker,
151
155
  TDropdown, TEditableTable, TEmpty, TFieldGroup, TFileUpload, TFontPicker, TFormField, TIcon, TIconPicker, TImageUpload,
152
- TInput, TInputNumber, TInputTags, TLogView, TModal, TNav, TNavPositionPicker, TPage, TPagination,
156
+ TInput, TInputNumber, TInputTags, TListBox, TLogView, TModal, TNav, TNavPositionPicker, TPage, TPagination,
153
157
  TPanelList, TProgress, TRadioGroup, TRadiusPicker, TRating, TScaledFrame, TSearchButton, TSectionLayout,
154
158
  TSelect, TSelectMenu, TSeparator, TShell, TSkeleton, TSlideover, TSparkline, TSpinner, TSplitter, TStatCard,
155
159
  TSteps, TSwitch, TTable, TTabs, TTextarea, TThemeBasePicker, TThemeScope, TToaster, TTooltip,
@@ -174,6 +178,7 @@ declare module 'vue' {
174
178
  TButton: typeof TButton
175
179
  TCard: typeof TCard
176
180
  TCheckbox: typeof TCheckbox
181
+ TCheckboxTree: typeof TCheckboxTree
177
182
  TColorModeButton: typeof TColorModeButton
178
183
  TCommandPalette: typeof TCommandPalette
179
184
  TContainerPicker: typeof TContainerPicker
@@ -194,6 +199,7 @@ declare module 'vue' {
194
199
  TInput: typeof TInput
195
200
  TInputNumber: typeof TInputNumber
196
201
  TInputTags: typeof TInputTags
202
+ TListBox: typeof TListBox
197
203
  TLogView: typeof TLogView
198
204
  TModal: typeof TModal
199
205
  TNav: typeof TNav
package/src/types.ts CHANGED
@@ -60,6 +60,14 @@ export interface AdvancedSelectItem<T = unknown> extends SelectItem<T> {
60
60
  avatar?: string
61
61
  }
62
62
 
63
+ /** An item of a TCheckboxTree: a leaf, or a parent with children. */
64
+ export interface TreeItem<T = string> {
65
+ label: string
66
+ value: T
67
+ children?: TreeItem<T>[]
68
+ disabled?: boolean
69
+ }
70
+
63
71
  export interface TabItem {
64
72
  label: string
65
73
  value: string