occulite-ui 0.0.0-stage → 0.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 (105) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +290 -2
  3. package/THIRD_PARTY_NOTICES.md +36 -0
  4. package/dist/clipboard.d.ts +26 -0
  5. package/dist/clipboard.js +70 -0
  6. package/dist/components/ContextMenu.svelte +158 -0
  7. package/dist/components/ContextMenu.svelte.d.ts +14 -0
  8. package/dist/components/DataTable.svelte +1990 -0
  9. package/dist/components/DataTable.svelte.d.ts +154 -0
  10. package/dist/components/Dialog.svelte +420 -0
  11. package/dist/components/Dialog.svelte.d.ts +55 -0
  12. package/dist/components/GithubLink.svelte +74 -0
  13. package/dist/components/GithubLink.svelte.d.ts +16 -0
  14. package/dist/components/Header.svelte +196 -0
  15. package/dist/components/Header.svelte.d.ts +42 -0
  16. package/dist/components/Icon.svelte +51 -0
  17. package/dist/components/Icon.svelte.d.ts +12 -0
  18. package/dist/components/LanguageSwitch.svelte +63 -0
  19. package/dist/components/LanguageSwitch.svelte.d.ts +17 -0
  20. package/dist/components/Loader.svelte +65 -0
  21. package/dist/components/Loader.svelte.d.ts +9 -0
  22. package/dist/components/MultiSelect.svelte +467 -0
  23. package/dist/components/MultiSelect.svelte.d.ts +28 -0
  24. package/dist/components/Notices.svelte +146 -0
  25. package/dist/components/Notices.svelte.d.ts +16 -0
  26. package/dist/components/PrimaryToolbarButton.svelte +189 -0
  27. package/dist/components/PrimaryToolbarButton.svelte.d.ts +34 -0
  28. package/dist/components/Tabs.svelte +118 -0
  29. package/dist/components/Tabs.svelte.d.ts +16 -0
  30. package/dist/components/ThemeSwitch.svelte +46 -0
  31. package/dist/components/ThemeSwitch.svelte.d.ts +11 -0
  32. package/dist/components/Toolbar.svelte +57 -0
  33. package/dist/components/Toolbar.svelte.d.ts +11 -0
  34. package/dist/components/ToolbarButton.svelte +126 -0
  35. package/dist/components/ToolbarButton.svelte.d.ts +22 -0
  36. package/dist/components/Tooltip.svelte +106 -0
  37. package/dist/components/Tooltip.svelte.d.ts +31 -0
  38. package/dist/components/TooltipBubble.svelte +65 -0
  39. package/dist/components/TooltipBubble.svelte.d.ts +18 -0
  40. package/dist/components/contextMenu.d.ts +19 -0
  41. package/dist/components/contextMenu.js +6 -0
  42. package/dist/components/dialogGeometry.d.ts +91 -0
  43. package/dist/components/dialogGeometry.js +140 -0
  44. package/dist/components/index.d.ts +22 -0
  45. package/dist/components/index.js +22 -0
  46. package/dist/components/multiSelect.d.ts +27 -0
  47. package/dist/components/multiSelect.js +26 -0
  48. package/dist/components/navigation.d.ts +34 -0
  49. package/dist/components/navigation.js +19 -0
  50. package/dist/components/tooltip.d.ts +29 -0
  51. package/dist/components/tooltip.js +27 -0
  52. package/dist/css/app.css +3 -0
  53. package/dist/css/base.css +84 -0
  54. package/dist/css/tokens.css +170 -0
  55. package/dist/dialog/index.d.ts +6 -0
  56. package/dist/dialog/index.js +6 -0
  57. package/dist/embedding/ShellEmbedding.svelte.d.ts +27 -0
  58. package/dist/embedding/ShellEmbedding.svelte.js +79 -0
  59. package/dist/embedding/constants.d.ts +23 -0
  60. package/dist/embedding/constants.js +54 -0
  61. package/dist/embedding/index.d.ts +2 -0
  62. package/dist/embedding/index.js +2 -0
  63. package/dist/externalLink.d.ts +15 -0
  64. package/dist/externalLink.js +20 -0
  65. package/dist/format.d.ts +6 -0
  66. package/dist/format.js +16 -0
  67. package/dist/i18n/i18n.svelte.d.ts +112 -0
  68. package/dist/i18n/i18n.svelte.js +211 -0
  69. package/dist/i18n/index.d.ts +3 -0
  70. package/dist/i18n/index.js +3 -0
  71. package/dist/i18n/messages.d.ts +9 -0
  72. package/dist/i18n/messages.js +58 -0
  73. package/dist/i18n/translator.d.ts +65 -0
  74. package/dist/i18n/translator.js +99 -0
  75. package/dist/icons/index.d.ts +17 -0
  76. package/dist/icons/index.js +11 -0
  77. package/dist/index.d.ts +16 -0
  78. package/dist/index.js +16 -0
  79. package/dist/notices/NoticesStore.svelte.d.ts +64 -0
  80. package/dist/notices/NoticesStore.svelte.js +127 -0
  81. package/dist/notices/index.d.ts +1 -0
  82. package/dist/notices/index.js +1 -0
  83. package/dist/storage.d.ts +11 -0
  84. package/dist/storage.js +27 -0
  85. package/dist/table/LocalColumnWidths.svelte.d.ts +32 -0
  86. package/dist/table/LocalColumnWidths.svelte.js +91 -0
  87. package/dist/table/columnMeasure.d.ts +24 -0
  88. package/dist/table/columnMeasure.js +82 -0
  89. package/dist/table/columnWidths.d.ts +60 -0
  90. package/dist/table/columnWidths.js +86 -0
  91. package/dist/table/dataTableContext.d.ts +26 -0
  92. package/dist/table/dataTableContext.js +16 -0
  93. package/dist/table/index.d.ts +6 -0
  94. package/dist/table/index.js +6 -0
  95. package/dist/table/metrics.d.ts +10 -0
  96. package/dist/table/metrics.js +10 -0
  97. package/dist/table/tableModel.d.ts +306 -0
  98. package/dist/table/tableModel.js +437 -0
  99. package/dist/theme/choices.d.ts +8 -0
  100. package/dist/theme/choices.js +6 -0
  101. package/dist/theme/index.d.ts +2 -0
  102. package/dist/theme/index.js +2 -0
  103. package/dist/theme/theme.svelte.d.ts +60 -0
  104. package/dist/theme/theme.svelte.js +108 -0
  105. package/package.json +137 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sebastian Raff
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,291 @@
1
- # Temporary Holding Version
1
+ # occulite-ui
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ openccu-lite's look for apps and addons: a Svelte 5 UI kit with the design tokens, a light and a
4
+ blue-grey dark theme, German and English strings, a virtualised `DataTable`, dialogs, menus,
5
+ tooltips, toasts, toolbars and the theme and language handling - and the frame side of
6
+ openccu-lite's embedding contract, so a page inside the system's shell follows the shell's theme
7
+ and language.
8
+
9
+ Its consumers are [homematic-manager](https://github.com/hobbyquaker/homematic-manager), the
10
+ system's own web interface ([occulited](https://github.com/openccu-lite/occulited)) and the addons
11
+ for openccu-lite and the classic CCU.
12
+
13
+ **Status: 0.x.** The API is settling; in 0.x a minor version may break (the
14
+ [CHANGELOG](CHANGELOG.md) says what and how to migrate), a patch never does. The CSS prefixes
15
+ `--hmm-*`, `hmm-*` and `ol-*` stay through 0.x and are renamed before 1.0.
16
+
17
+ ## Install
18
+
19
+ ```sh
20
+ npm install occulite-ui
21
+ ```
22
+
23
+ `svelte` (^5.40) is a peer dependency; the package has no runtime dependencies. It is a source
24
+ package: the `.svelte` files are compiled by your app's Svelte, so there is one Svelte runtime and
25
+ your compiler options apply.
26
+
27
+ ## Vite setup
28
+
29
+ Vite with `@sveltejs/vite-plugin-svelte` is all it needs - the package carries the `svelte` export
30
+ condition. TypeScript consumers need `"moduleResolution": "bundler"` (or `node16`/`nodenext`).
31
+
32
+ When you link a local checkout of the kit (`npm link`, a workspace), keep a single Svelte:
33
+
34
+ ```ts
35
+ // vite.config.ts
36
+ export default defineConfig({
37
+ plugins: [svelte()],
38
+ resolve: {dedupe: ['svelte']},
39
+ });
40
+ ```
41
+
42
+ ## Usage
43
+
44
+ ```ts
45
+ // main.ts
46
+ import 'occulite-ui/app.css';
47
+ import {mount} from 'svelte';
48
+ import {addMessages, initI18n} from 'occulite-ui/i18n';
49
+ import {initTheme} from 'occulite-ui/theme';
50
+
51
+ import App from './App.svelte';
52
+ import {MESSAGES} from './messages.js';
53
+
54
+ addMessages(MESSAGES); // your strings, over the kit's; yours win
55
+ initI18n({storageKey: 'myapp.language'}); // follows openccu-lite's shell while embedded
56
+ initTheme({storageKey: 'myapp.theme'}); // 'system' | 'light' | 'dark'
57
+
58
+ mount(App, {target: document.getElementById('app')!});
59
+ ```
60
+
61
+ ```svelte
62
+ <script lang="ts">
63
+ import {DataTable, Header, Notices, ThemeSwitch} from 'occulite-ui';
64
+ import {NoticesStore} from 'occulite-ui/notices';
65
+ import {t} from 'occulite-ui/i18n';
66
+ import {cycleTheme, theme} from 'occulite-ui/theme';
67
+
68
+ const notices = new NoticesStore();
69
+ let tab = $state('devices');
70
+ </script>
71
+
72
+ {#if !theme.followsShell}
73
+ <ThemeSwitch theme={theme.choice} onclick={cycleTheme} />
74
+ {/if}
75
+ <Header title="My app" items={[{id: 'devices', label: t('Devices')}]} bind:active={tab} />
76
+ <DataTable rows={[]} columns={[{key: 'name', label: t('Name')}]} getId={(row) => row.id} />
77
+ <Notices notices={notices.visible} ondismiss={(id) => notices.dismiss(id)} />
78
+ ```
79
+
80
+ ### What is in it
81
+
82
+ | Import | What |
83
+ | ------------------------------------------------ | ----------------------------------------------------------------------------------------------------- |
84
+ | `occulite-ui` | every component and helper below (tree-shakable) |
85
+ | `occulite-ui/components/<Name>` | one component |
86
+ | `occulite-ui/app.css` | `tokens.css` + `base.css`, import once |
87
+ | `occulite-ui/tokens.css`, `occulite-ui/base.css` | the custom properties alone; the global rules alone |
88
+ | `occulite-ui/i18n` | `t`, `has`, `translations`, `addMessages`, `initI18n`, `setLanguage`, `i18n.language`, the translator |
89
+ | `occulite-ui/theme` | `theme`, `initTheme`, `setTheme`, `cycleTheme`, `isDark`, `resolvedTheme`, `applyTheme` |
90
+ | `occulite-ui/embedding` | `ShellEmbedding`, `shellEmbedding()`, the contract's constants |
91
+ | `occulite-ui/table` | the table model, column widths, `LocalColumnWidths`, `setDataTableEnvironment`, `ROW_HEIGHT` |
92
+ | `occulite-ui/dialog` | `Dialog` and its geometry helpers |
93
+ | `occulite-ui/notices` | `NoticesStore` |
94
+ | `occulite-ui/icons` | icons as data (`IconData`), drawn by `Icon` and the toolbar buttons |
95
+ | `occulite-ui/format`, `occulite-ui/clipboard` | time and duration formatting; copying text |
96
+
97
+ Every visible text of a component is a prop; when it is absent, the component falls back to the
98
+ kit's catalogue in the page's language, and re-renders when the language changes.
99
+
100
+ A denser app sets `--hmm-row-height` to another height (26 px, say) and passes the same number to
101
+ `DataTable`'s `rowHeight`: the virtualiser needs it as a number.
102
+
103
+ ### The bar on top: `Header`
104
+
105
+ `Header` is the bar homematic-manager has on top: the app's name, its places as tabs, and a group
106
+ of the page's own items at the right. One flat line of `--hmm-header-height` (36 px) on the header
107
+ ground with a hairline under it and 10 px at the sides; the tabs are as tall as the bar and lie
108
+ on the hairline, everything else stands in the middle of the line. Below 64rem (1024 px) the bar
109
+ is two such lines - the name and the items, then the tabs, which scroll in their strip and keep
110
+ the open one in view (`narrow={false}` keeps one line, for an app with a minimum width of its own).
111
+
112
+ ```svelte
113
+ <script lang="ts">
114
+ import {Header, ThemeSwitch, type NavItem} from 'occulite-ui';
115
+
116
+ const items: NavItem[] = [
117
+ {id: 'programs', label: t('Programs'), href: '#/programs'},
118
+ {id: 'variables', label: t('System variables'), badge: alarms || undefined},
119
+ ];
120
+ let active = $state('programs');
121
+ </script>
122
+
123
+ <Header title="Logic" {items} bind:active onnavigate={(id) => show(id)}>
124
+ {#snippet actions()}
125
+ <span class="hmm-header-text" title={version}>{version}</span>
126
+ <ThemeSwitch theme={theme.choice} onclick={cycleTheme} />
127
+ {/snippet}
128
+ </Header>
129
+ ```
130
+
131
+ - The navigation is data: `NavItem` is `{id, label, badge?, icon?, href?}`, `active` the open id
132
+ (an id the items do not have shows the first), `onnavigate(id, item)` the user's choice. An app
133
+ that must ask before it leaves a place binds `active` to a getter and a setter
134
+ (`bind:active={() => tab, go}`) and decides in the setter. Nothing of the model is the bar's:
135
+ another navigation component - a sidebar, a drawer - takes the same three props, so an app
136
+ swaps one for the other without touching its routes. The header's tabs are buttons and show
137
+ the label and the badge; `icon` and `href` are for a navigation that has room for a mark or
138
+ draws links.
139
+ - `brand` (a snippet) replaces the name: a logo, a control of the app's own.
140
+ - `actions` is the group at the right. Controls keep their box (make them 24 px tall, as
141
+ `ThemeSwitch` and the toolbar buttons are); an element with the class `hmm-header-text` is
142
+ small, muted text that is cut when the line is short - give it a `title`.
143
+ - `activeNavId`, `navItem` and `neighbourNavItem` are the model's helpers.
144
+
145
+ ### Tables: `DataTable`
146
+
147
+ One table for every app: a header band (actions at the left, a search and a count at the right),
148
+ the column labels, a row of filter fields, and rows of `--hmm-row-height` (30 px) in a body that
149
+ scrolls under a head that stays. The rows are virtualised, so the table needs a height: it fills
150
+ its parent (give that a height, or make it a flex item that has one), or takes `height` in pixels.
151
+ A column has its width (`width` as a weight, `fixed: true` for pixels, `minWidth`), so nothing
152
+ moves when the table is sorted or filtered; a text that does not fit is cut and whole in its
153
+ tooltip. When the columns need more than the table has - a phone - the body scrolls sideways
154
+ inside the table and the head follows; the page never does.
155
+
156
+ ```svelte
157
+ <script lang="ts">
158
+ import {DataTable} from 'occulite-ui';
159
+ import type {DataTableColumn, SortState} from 'occulite-ui/table';
160
+
161
+ const columns: DataTableColumn<Variable>[] = [
162
+ {key: 'name', label: t('Name'), width: 260, minWidth: 150},
163
+ {
164
+ key: 'type',
165
+ label: t('Type'),
166
+ width: 116,
167
+ fixed: true,
168
+ value: (row) => t('type.' + row.type), // what the cell shows and the search reads
169
+ filterValue: (row) => row.type, // what the column's own filter compares
170
+ filterOptions: TYPES.map((id) => ({value: id, label: t('type.' + id)})),
171
+ },
172
+ {
173
+ key: 'time',
174
+ label: t('Last written'),
175
+ width: 150,
176
+ fixed: true,
177
+ value: (row) => formatDate(row.time),
178
+ sortValue: (row) => Date.parse(row.time), // what the column sorts by
179
+ emptyLast: true,
180
+ },
181
+ ];
182
+ let sort = $state<SortState | undefined>();
183
+ let filter = $state('');
184
+ let columnFilters = $state<Record<string, string>>({});
185
+ </script>
186
+
187
+ <DataTable
188
+ {rows}
189
+ {columns}
190
+ getId={(row) => row.id}
191
+ bind:sort
192
+ bind:filter
193
+ bind:columnFilters
194
+ searchBox
195
+ countText={t('{n} variables', {n: rows.length})}
196
+ />
197
+ ```
198
+
199
+ - **Sorting.** A click on a column label sorts ascending, the next one descending, the third
200
+ shows the rows as they came. Numbers sort as numbers, text with German collation (`locale`,
201
+ `'de'` by default: umlauts with their base letters, digits as numbers - the same order for
202
+ English text, and the same on every machine), empty cells last (`emptyLast` keeps them last in
203
+ both directions). The sort is stable: rows that compare equal keep the order they came in. A
204
+ column that shows one thing and sorts by another names it as `sortValue`.
205
+ - **Filtering.** `searchBox` draws a search field in the band: every word of it has to be
206
+ somewhere in the row's filterable columns. The filter row (`columnFilterRow`, on by default)
207
+ has a field per column - "contains" - or, for a column with `filterOptions`, a list of choices
208
+ that matches exactly; `filterValue` is what the column's own filter compares where that is not
209
+ the text of the cell (an id behind a translated label). When the filters hide every row the
210
+ table says so and offers to clear them.
211
+ - **State from outside.** `sort`, `filter` and `columnFilters` are bindable props, and
212
+ `onsortchange` / `onfilterchange` report what the user changed. An app that keeps the three in
213
+ its address sets them from it and writes them back, and a reload, a link and the back button
214
+ show the same rows in the same order.
215
+ - **Cells.** Without a `cell` snippet a cell is the column's text; with one the app draws it - a
216
+ pill, a field, buttons - and `value` stays what is sorted, searched and filtered. `rowTestId`
217
+ and `rowClass` put a test id and the app's own classes on a row.
218
+ - **More.** Sub-rows with columns of their own, selection (click, ctrl, shift), a context menu,
219
+ renaming, a copy button per column, resizable columns kept by `setDataTableEnvironment`, column
220
+ groups: see the props of `DataTable.svelte` and `occulite-ui/table`.
221
+
222
+ ### Inside openccu-lite's shell
223
+
224
+ The system frames an addon page as a same-origin iframe, puts `theme=` and `lang=` on the frame
225
+ URL and posts `{type: 'openccu-lite:theme', theme, lang}` to it on every change. With
226
+ `follow: 'shell'` (the default) `initTheme` and `initI18n` follow that, and `theme.followsShell`
227
+ tells a page to hide its own theme switch. A page opened on its own (a tab, a classic CCU, a
228
+ desktop app) keeps its own choice; `follow: 'none'` ignores the shell altogether. The kit reads the
229
+ shell's `ol-lang` cookie but never writes it.
230
+
231
+ ### No browser dialogs
232
+
233
+ Never `window.confirm`, `prompt` or `alert`: they block the page, cannot be styled or translated
234
+ and are suppressed in some frames. Ask through a dialog, tell through a notice. The kit's own
235
+ eslint config bans the three; the same two rules in an app's config:
236
+
237
+ ```js
238
+ // eslint.config.js, in a config object's `rules`
239
+ 'no-restricted-globals': ['error', 'confirm', 'prompt', 'alert'],
240
+ 'no-restricted-properties': [
241
+ 'error',
242
+ {object: 'window', property: 'confirm'},
243
+ {object: 'window', property: 'prompt'},
244
+ {object: 'window', property: 'alert'},
245
+ ],
246
+ ```
247
+
248
+ ## Support
249
+
250
+ - Svelte `^5.40` (CI tests the floor and the current version), Node 22 and 24 for the tooling.
251
+ - Evergreen browsers (Chromium, Firefox, Safari of the last two years).
252
+
253
+ ## Development
254
+
255
+ ```sh
256
+ npm ci
257
+ npx playwright install --with-deps chromium # once: the component tests run in a real chromium
258
+ npm run lint && npm run typecheck && npm test
259
+ npm run build && npm run publint && npm run pack:check && npm run size
260
+ ```
261
+
262
+ `npm run test:jsdom` runs the same tests without a browser, as a fallback; CI reports on the
263
+ browser run. `npm run size` builds three typical import sets with Vite and fails when one is more
264
+ than 10 % over `size-budget.json` (`node scripts/size-report.mjs --update` rewrites the budget).
265
+
266
+ ## Releasing
267
+
268
+ The first publish is done by hand, because npm's trusted publishing can only be configured for a
269
+ package that exists:
270
+
271
+ 1. On a clean checkout of `main` with green CI: `npm ci && npm test && npm run build && npm run pack:check`.
272
+ 2. `npm login` (an account with publish rights; two-factor authentication on), then
273
+ `npm publish --access public`. The `prepack` script builds `dist/` again; the tarball is the one
274
+ `npm run pack:check` listed.
275
+ 3. Tag the commit: `git tag -a v0.1.0 -m v0.1.0 && git push origin v0.1.0`, and create the GitHub
276
+ release with the title `v0.1.0` and the CHANGELOG section as its notes.
277
+ 4. On npmjs.com, package settings → _Trusted publishing_: add GitHub Actions with the owner
278
+ `hobbyquaker`, the repository `occulite-ui` and the workflow `release.yml`. Then, under
279
+ _Publishing access_, require two-factor authentication and disallow tokens.
280
+ 5. In the GitHub repository, set the Actions variable `NPM_TRUSTED_PUBLISHING` to `true`.
281
+
282
+ From then on a release is: bump `version` in `package.json`, move the CHANGELOG's section from
283
+ "unreleased" to the date, commit, push a `v<version>` tag. `release.yml` tests, checks that the tag
284
+ matches the version, publishes with provenance (`next` for a prerelease, `latest` otherwise) and
285
+ creates the GitHub release, titled with the tag.
286
+
287
+ ## License
288
+
289
+ MIT, see [LICENSE](LICENSE). The code was written for homematic-manager and the openccu-lite
290
+ web interface by the same author and is relicensed MIT here. The vendored GitHub mark is listed in
291
+ [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
@@ -0,0 +1,36 @@
1
+ # Third-party notices
2
+
3
+ occulite-ui is MIT licensed (see `LICENSE`). It ships no third-party code. It does contain one
4
+ vendored drawing:
5
+
6
+ ## GitHub mark (Octicons `mark-github`)
7
+
8
+ `src/lib/icons/index.ts` (`iconGithub`) and `src/lib/components/GithubLink.svelte` draw the path of
9
+ Octicons' `mark-github`, from https://github.com/primer/octicons.
10
+
11
+ ```
12
+ MIT License
13
+
14
+ Copyright (c) GitHub Inc.
15
+
16
+ Permission is hereby granted, free of charge, to any person obtaining a copy
17
+ of this software and associated documentation files (the "Software"), to deal
18
+ in the Software without restriction, including without limitation the rights
19
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
+ copies of the Software, and to permit persons to whom the Software is
21
+ furnished to do so, subject to the following conditions:
22
+
23
+ The above copyright notice and this permission notice shall be included in all
24
+ copies or substantial portions of the Software.
25
+
26
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32
+ SOFTWARE.
33
+ ```
34
+
35
+ The GitHub name and mark are trademarks of GitHub, Inc.; the mark is used only to link to a
36
+ project's GitHub page.
@@ -0,0 +1,26 @@
1
+ /**
2
+ * A text onto the clipboard, from the copy button of a grid cell.
3
+ *
4
+ * `navigator.clipboard` exists only in a secure context - HTTPS, `localhost`, the Electron window.
5
+ * The CCU addon is usually opened over plain HTTP (`http://<ccu>/addons/hmm/`), where the property
6
+ * is simply missing, and an embedding frame without `clipboard-write` makes `writeText` refuse. So
7
+ * the old way is kept behind it: the text in a field of its own, selected, and the browser's `copy`
8
+ * command, which still works from the click that asked for it.
9
+ */
10
+ /** Which way the text went: the Clipboard API, the selected field, or neither. */
11
+ export type CopyOutcome = 'clipboard' | 'selection' | 'failed';
12
+ export interface CopyOptions {
13
+ /**
14
+ * Where the fallback's field is put for the moment of the copy - inside the grid, so a modal
15
+ * dialog or an `inert` elsewhere cannot stand between the field and the command. The body when
16
+ * absent.
17
+ */
18
+ readonly host?: HTMLElement | undefined;
19
+ }
20
+ export declare function copyText(text: string, options?: CopyOptions): Promise<CopyOutcome>;
21
+ /**
22
+ * The fallback: a read-only field holding the text, selected, `document.execCommand('copy')`, and
23
+ * the field gone again. The focus goes back where it was - the copy button - so the grid's keyboard
24
+ * position is not lost.
25
+ */
26
+ export declare function copyBySelection(text: string, host?: HTMLElement): boolean;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * A text onto the clipboard, from the copy button of a grid cell.
3
+ *
4
+ * `navigator.clipboard` exists only in a secure context - HTTPS, `localhost`, the Electron window.
5
+ * The CCU addon is usually opened over plain HTTP (`http://<ccu>/addons/hmm/`), where the property
6
+ * is simply missing, and an embedding frame without `clipboard-write` makes `writeText` refuse. So
7
+ * the old way is kept behind it: the text in a field of its own, selected, and the browser's `copy`
8
+ * command, which still works from the click that asked for it.
9
+ */
10
+ /** Read at the moment of the copy: the property is absent where the page is not a secure context. */
11
+ function clipboardOf() {
12
+ return globalThis.navigator?.clipboard;
13
+ }
14
+ export async function copyText(text, options = {}) {
15
+ const clipboard = clipboardOf();
16
+ if (typeof clipboard?.writeText === 'function') {
17
+ try {
18
+ await clipboard.writeText(text);
19
+ return 'clipboard';
20
+ }
21
+ catch {
22
+ // refused - no permission, a frame without clipboard-write: the selection below still can
23
+ }
24
+ }
25
+ return copyBySelection(text, options.host) ? 'selection' : 'failed';
26
+ }
27
+ /**
28
+ * The fallback: a read-only field holding the text, selected, `document.execCommand('copy')`, and
29
+ * the field gone again. The focus goes back where it was - the copy button - so the grid's keyboard
30
+ * position is not lost.
31
+ */
32
+ export function copyBySelection(text, host = document.body) {
33
+ const owner = host.ownerDocument;
34
+ const previous = owner.activeElement;
35
+ const field = owner.createElement('textarea');
36
+ field.value = text;
37
+ field.readOnly = true;
38
+ field.tabIndex = -1;
39
+ field.setAttribute('aria-hidden', 'true');
40
+ // in the window, or `select()` does nothing in some browsers; invisible, and out of the layout
41
+ Object.assign(field.style, {
42
+ position: 'fixed',
43
+ top: '0',
44
+ left: '0',
45
+ width: '1px',
46
+ height: '1px',
47
+ padding: '0',
48
+ border: '0',
49
+ opacity: '0',
50
+ pointerEvents: 'none',
51
+ });
52
+ host.append(field);
53
+ try {
54
+ field.focus({ preventScroll: true });
55
+ field.select();
56
+ // iOS selects nothing in a read-only field without the explicit range
57
+ field.setSelectionRange(0, text.length);
58
+ // eslint-disable-next-line @typescript-eslint/no-deprecated -- the only copy there is outside a secure context
59
+ return owner.execCommand('copy');
60
+ }
61
+ catch {
62
+ return false;
63
+ }
64
+ finally {
65
+ field.remove();
66
+ if (previous instanceof HTMLElement || previous instanceof SVGElement) {
67
+ previous.focus({ preventScroll: true });
68
+ }
69
+ }
70
+ }
@@ -0,0 +1,158 @@
1
+ <script lang="ts">
2
+ import {t as kitT} from '../i18n/i18n.svelte.js';
3
+ import {clampMenuPosition, type ContextMenuItem} from './contextMenu.js';
4
+
5
+ interface Props {
6
+ items: ContextMenuItem[];
7
+ open?: boolean;
8
+ x?: number;
9
+ y?: number;
10
+ onselect?: ((id: string) => void) | undefined;
11
+ onclose?: (() => void) | undefined;
12
+ label?: string | undefined;
13
+ testId?: string | undefined;
14
+ }
15
+
16
+ let {
17
+ items,
18
+ open = $bindable(false),
19
+ x = 0,
20
+ y = 0,
21
+ onselect = undefined,
22
+ onclose = undefined,
23
+ label: labelProp = undefined,
24
+ testId = undefined,
25
+ }: Props = $props();
26
+
27
+ const label = $derived(labelProp ?? kitT('Menu'));
28
+
29
+ let menu = $state<HTMLDivElement | undefined>(undefined);
30
+ let position = $state({x: 0, y: 0});
31
+
32
+ /**
33
+ * The per-row menu the 2.x grids reached through the pager buttons only. Everything it offers
34
+ * is also on the toolbar, so a browser that swallows the context menu loses nothing.
35
+ */
36
+ $effect(() => {
37
+ if (!open || !menu) {
38
+ return;
39
+ }
40
+ const rect = menu.getBoundingClientRect();
41
+ position = clampMenuPosition(
42
+ x,
43
+ y,
44
+ {width: rect.width || 180, height: rect.height || 0},
45
+ {width: window.innerWidth, height: window.innerHeight},
46
+ );
47
+ const first = menu.querySelector<HTMLButtonElement>('button:not(:disabled)');
48
+ first?.focus();
49
+ });
50
+
51
+ function close(): void {
52
+ open = false;
53
+ onclose?.();
54
+ }
55
+
56
+ function choose(item: ContextMenuItem): void {
57
+ if (item.disabled === true || item.separator === true) {
58
+ return;
59
+ }
60
+ close();
61
+ onselect?.(item.id);
62
+ }
63
+
64
+ /**
65
+ * Escape closes the open menu, and only an open one. The listener is on the window for as long
66
+ * as the page is mounted; when it took every Escape, its `preventDefault` kept the key from the
67
+ * browser, a modal `<dialog>` never got its `cancel` event, and no dialog closed on Escape while
68
+ * the Devices or Links tab was shown.
69
+ */
70
+ function onKeyDown(event: KeyboardEvent): void {
71
+ if (open && event.key === 'Escape') {
72
+ event.preventDefault();
73
+ close();
74
+ }
75
+ }
76
+ </script>
77
+
78
+ <svelte:window
79
+ onkeydown={onKeyDown}
80
+ onmousedown={(event) => {
81
+ if (open && menu && event.target instanceof Node && !menu.contains(event.target)) {
82
+ close();
83
+ }
84
+ }}
85
+ />
86
+
87
+ {#if open}
88
+ <div
89
+ class="hmm-context-menu"
90
+ bind:this={menu}
91
+ role="menu"
92
+ aria-label={label}
93
+ data-testid={testId}
94
+ style:left={`${position.x}px`}
95
+ style:top={`${position.y}px`}
96
+ >
97
+ {#each items as item (item.id)}
98
+ {#if item.separator === true}
99
+ <div class="hmm-context-separator" role="separator"></div>
100
+ {:else}
101
+ <button
102
+ type="button"
103
+ class="hmm-context-item"
104
+ class:hmm-context-danger={item.danger === true}
105
+ role="menuitem"
106
+ disabled={item.disabled === true}
107
+ onclick={() => choose(item)}
108
+ >
109
+ {item.label ?? item.id}
110
+ </button>
111
+ {/if}
112
+ {/each}
113
+ </div>
114
+ {/if}
115
+
116
+ <style>
117
+ .hmm-context-menu {
118
+ position: fixed;
119
+ z-index: 100;
120
+ min-width: 180px;
121
+ padding: 2px 0;
122
+ border: 1px solid var(--hmm-border-strong);
123
+ border-radius: var(--hmm-radius);
124
+ background: var(--hmm-bg);
125
+ box-shadow: var(--hmm-shadow-menu);
126
+ }
127
+
128
+ .hmm-context-item {
129
+ display: block;
130
+ width: 100%;
131
+ padding: 3px 10px;
132
+ border: none;
133
+ background: none;
134
+ font: inherit;
135
+ text-align: left;
136
+ cursor: pointer;
137
+ }
138
+
139
+ .hmm-context-item:hover:not(:disabled) {
140
+ background: var(--hmm-row-selected);
141
+ color: var(--hmm-row-selected-text);
142
+ }
143
+
144
+ .hmm-context-item:disabled {
145
+ opacity: 0.45;
146
+ cursor: default;
147
+ }
148
+
149
+ .hmm-context-danger {
150
+ color: var(--hmm-error);
151
+ }
152
+
153
+ .hmm-context-separator {
154
+ height: 1px;
155
+ margin: 2px 0;
156
+ background: var(--hmm-border-muted);
157
+ }
158
+ </style>
@@ -0,0 +1,14 @@
1
+ import { type ContextMenuItem } from './contextMenu.js';
2
+ interface Props {
3
+ items: ContextMenuItem[];
4
+ open?: boolean;
5
+ x?: number;
6
+ y?: number;
7
+ onselect?: ((id: string) => void) | undefined;
8
+ onclose?: (() => void) | undefined;
9
+ label?: string | undefined;
10
+ testId?: string | undefined;
11
+ }
12
+ declare const ContextMenu: import("svelte").Component<Props, {}, "open">;
13
+ type ContextMenu = ReturnType<typeof ContextMenu>;
14
+ export default ContextMenu;