@sveltia/ui 0.85.0 → 0.85.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.
@@ -138,7 +138,14 @@
138
138
  });
139
139
  </script>
140
140
 
141
- <div {...restProps} role="none" class="sui code-editor" class:flex {hidden}>
141
+ <div
142
+ {...restProps}
143
+ role="none"
144
+ class="sui code-editor"
145
+ class:flex
146
+ {hidden}
147
+ style:--sui-text-editor-toolbar-height="{editorStore.toolbarHeight}px"
148
+ >
142
149
  {#if showLanguageSwitcher}
143
150
  <CodeEditorToolbar {disabled} {readonly} />
144
151
  {/if}
@@ -162,4 +169,14 @@
162
169
  }
163
170
  .code-editor.flex:not([hidden]) {
164
171
  display: block;
172
+ }
173
+ .code-editor {
174
+ --sui-text-editor-toolbar-top: var(--sui-text-editor-inner-toolbar-top, 0px);
175
+ --sui-text-editor-toolbar-z-index: var(--sui-text-editor-inner-toolbar-z-index, 100);
176
+ }
177
+ .code-editor > :global(*) {
178
+ --sui-text-editor-inner-toolbar-top: calc(
179
+ var(--sui-text-editor-toolbar-top) + var(--sui-text-editor-toolbar-height)
180
+ );
181
+ --sui-text-editor-inner-toolbar-z-index: calc(var(--sui-text-editor-toolbar-z-index) - 1);
165
182
  }</style>
@@ -1,6 +1,6 @@
1
1
  <script>
2
2
  import { $getRoot as getRoot, HISTORY_MERGE_TAG } from 'lexical';
3
- import { getContext, onMount } from 'svelte';
3
+ import { getContext, onMount, untrack } from 'svelte';
4
4
  import { IMPORT_UPDATE_TAG } from './constants.js';
5
5
  import { handleEditorMouseDown, initEditor } from './core.js';
6
6
 
@@ -133,6 +133,58 @@
133
133
  }
134
134
  };
135
135
 
136
+ // The state is updated within `untrack()` or a microtask below, because the browser fires these
137
+ // events synchronously while it removes a focused element from the DOM, e.g. a nested editor in a
138
+ // component being removed, in the middle of Svelte rendering, where a plain assignment would be
139
+ // reported as an unsafe mutation. See also `<FloatingLinkEditor>`.
140
+
141
+ /**
142
+ * Check if the given element is a dialog other than the one the editor is in, if any, like a menu
143
+ * opened from the toolbar of a nested editor, which is rendered outside the editor.
144
+ * @param {Element | null} dialog Dialog element.
145
+ * @returns {boolean} Result.
146
+ */
147
+ const isOtherDialog = (dialog) => !!dialog && !dialog.contains(lexicalRoot ?? null);
148
+
149
+ /**
150
+ * Track the focus, to tell whether it’s in an editor component, like a nested editor or a field
151
+ * of the component, rather than on the editor itself. The focus moving to another dialog doesn’t
152
+ * change anything.
153
+ * @param {FocusEvent} event `focusin` event.
154
+ */
155
+ const onFocusIn = ({ target }) => {
156
+ const node = /** @type {Node} */ (target);
157
+
158
+ if (lexicalRoot?.contains(node)) {
159
+ untrack(() => {
160
+ editorStore.componentHasFocus = node !== lexicalRoot;
161
+ });
162
+ } else if (!(node instanceof Element && isOtherDialog(node.closest('dialog')))) {
163
+ untrack(() => {
164
+ editorStore.componentHasFocus = false;
165
+ });
166
+ }
167
+ };
168
+
169
+ /**
170
+ * Track the focus leaving a removed element, which doesn’t fire `focusin`. The event also comes
171
+ * without a related target when a dialog opens, which only moves the focus into itself a moment
172
+ * later, so the state is kept while another modal dialog is open.
173
+ * @param {FocusEvent} event `focusout` event.
174
+ */
175
+ const onFocusOut = ({ relatedTarget }) => {
176
+ if (!relatedTarget) {
177
+ queueMicrotask(() => {
178
+ if (
179
+ document.activeElement === document.body &&
180
+ ![...document.querySelectorAll(':modal')].some(isOtherDialog)
181
+ ) {
182
+ editorStore.componentHasFocus = false;
183
+ }
184
+ });
185
+ }
186
+ };
187
+
136
188
  onMount(() => {
137
189
  const { editor, enabledTransformers, dispose } = initEditor(editorStore.config);
138
190
 
@@ -142,6 +194,8 @@
142
194
  lexicalRoot?.addEventListener('Update', onUpdate);
143
195
  lexicalRoot?.addEventListener('click', onClick);
144
196
  lexicalRoot?.addEventListener('mousedown', onMouseDown);
197
+ document.addEventListener('focusin', onFocusIn);
198
+ document.addEventListener('focusout', onFocusOut);
145
199
 
146
200
  // A change made by the user is converted to Markdown a moment later, so report it as pending
147
201
  // until then. An import, or an update that only changes how the content looks, e.g. the code
@@ -163,6 +217,8 @@
163
217
  lexicalRoot?.removeEventListener('Update', onUpdate);
164
218
  lexicalRoot?.removeEventListener('click', onClick);
165
219
  lexicalRoot?.removeEventListener('mousedown', onMouseDown);
220
+ document.removeEventListener('focusin', onFocusIn);
221
+ document.removeEventListener('focusout', onFocusOut);
166
222
  dispose();
167
223
  editor.setRootElement(null);
168
224
  editorStore.initialized = false;
@@ -198,7 +254,7 @@
198
254
  ></div>
199
255
 
200
256
  <style>.lexical-root {
201
- overflow: hidden;
257
+ overflow: clip;
202
258
  border: 1px solid var(--sui-textbox-border-color);
203
259
  border-radius: var(--sui-textbox-border-radius) !important;
204
260
  padding: var(--sui-textbox-multiline-padding);
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * Version of this package, used to resolve the prebuilt Shiki engine chunk from a CDN.
3
3
  */
4
- export const UI_VERSION: "0.85.0";
4
+ export const UI_VERSION: "0.85.2";
@@ -3,4 +3,4 @@
3
3
  /**
4
4
  * Version of this package, used to resolve the prebuilt Shiki engine chunk from a CDN.
5
5
  */
6
- export const UI_VERSION = '0.85.0';
6
+ export const UI_VERSION = '0.85.2';
@@ -39,6 +39,10 @@ export const createEditorStore = () => {
39
39
  let useRichText = $state(true);
40
40
  /** @type {HTMLTextAreaElement | undefined} */
41
41
  let textArea = $state();
42
+ /** @type {number} */
43
+ let toolbarHeight = $state(0);
44
+ /** @type {boolean} */
45
+ let componentHasFocus = $state(false);
42
46
  /** @type {boolean} */
43
47
  let hasConverterError = $state(false);
44
48
  /** @type {boolean} */
@@ -180,6 +184,18 @@ export const createEditorStore = () => {
180
184
  set textArea(newValue) {
181
185
  textArea = newValue;
182
186
  },
187
+ get toolbarHeight() {
188
+ return toolbarHeight;
189
+ },
190
+ set toolbarHeight(newValue) {
191
+ toolbarHeight = newValue;
192
+ },
193
+ get componentHasFocus() {
194
+ return componentHasFocus;
195
+ },
196
+ set componentHasFocus(newValue) {
197
+ componentHasFocus = newValue;
198
+ },
183
199
  get useRichText() {
184
200
  return useRichText;
185
201
  },
@@ -304,7 +304,14 @@
304
304
  });
305
305
  </script>
306
306
 
307
- <div {...restProps} role="none" class="sui text-editor" class:flex {hidden}>
307
+ <div
308
+ {...restProps}
309
+ role="none"
310
+ class="sui text-editor"
311
+ class:flex
312
+ {hidden}
313
+ style:--sui-text-editor-toolbar-height="{editorStore.toolbarHeight}px"
314
+ >
308
315
  <TextEditorToolbar {disabled} {readonly} />
309
316
  <LexicalRoot
310
317
  {...labelAttrs}
@@ -371,4 +378,14 @@
371
378
  border-start-end-radius: 0 !important;
372
379
  border-end-start-radius: var(--sui-textbox-border-radius) !important;
373
380
  border-end-end-radius: var(--sui-textbox-border-radius) !important;
381
+ }
382
+ .text-editor {
383
+ --sui-text-editor-toolbar-top: var(--sui-text-editor-inner-toolbar-top, 0px);
384
+ --sui-text-editor-toolbar-z-index: var(--sui-text-editor-inner-toolbar-z-index, 100);
385
+ }
386
+ .text-editor > :global(*) {
387
+ --sui-text-editor-inner-toolbar-top: calc(
388
+ var(--sui-text-editor-toolbar-top) + var(--sui-text-editor-toolbar-height)
389
+ );
390
+ --sui-text-editor-inner-toolbar-z-index: calc(var(--sui-text-editor-toolbar-z-index) - 1);
374
391
  }</style>
@@ -128,7 +128,11 @@
128
128
  );
129
129
  </script>
130
130
 
131
- <ToolbarWrapper disabled={disabled || readonly} ariaLabel={_('_sui.text_editor.text_editor')}>
131
+ <!-- Disabled while an editor component has focus, as the buttons only apply to this editor -->
132
+ <ToolbarWrapper
133
+ disabled={disabled || readonly || editorStore.componentHasFocus}
134
+ ariaLabel={_('_sui.text_editor.text_editor')}
135
+ >
132
136
  {#if canChangeBlock && blockLevelButtons.length > 1}
133
137
  <MenuButton
134
138
  aria-label={_('_sui.text_editor.show_text_style_options')}
@@ -1,8 +1,10 @@
1
1
  <script>
2
+ import { getContext } from 'svelte';
2
3
  import Toolbar from '../../toolbar/toolbar.svelte';
3
4
 
4
5
  /**
5
6
  * @import { Snippet } from 'svelte';
7
+ * @import { TextEditorStore } from '../../../typedefs.js';
6
8
  */
7
9
 
8
10
  /**
@@ -22,10 +24,32 @@
22
24
  ...restProps
23
25
  /* eslint-enable prefer-const */
24
26
  } = $props();
27
+
28
+ /** @type {TextEditorStore} */
29
+ const editorStore = getContext('editorStore');
30
+
31
+ /**
32
+ * Keep the toolbar height in the store, so the sticky toolbars of nested editors can be placed
33
+ * below this one.
34
+ * @param {HTMLElement} toolbar Toolbar element.
35
+ * @returns {() => void} Cleanup function.
36
+ */
37
+ const observeHeight = (toolbar) => {
38
+ const observer = new ResizeObserver(([entry]) => {
39
+ editorStore.toolbarHeight = entry.borderBoxSize[0].blockSize;
40
+ });
41
+
42
+ observer.observe(toolbar);
43
+
44
+ return () => {
45
+ observer.disconnect();
46
+ editorStore.toolbarHeight = 0;
47
+ };
48
+ };
25
49
  </script>
26
50
 
27
51
  <div role="none" class="wrapper">
28
- <Toolbar {...restProps} {disabled}>
52
+ <Toolbar {...restProps} {disabled} {@attach observeHeight}>
29
53
  {@render children?.()}
30
54
  </Toolbar>
31
55
  </div>
@@ -35,8 +59,8 @@
35
59
  }
36
60
  .wrapper :global([role=toolbar]) {
37
61
  position: sticky;
38
- top: 0;
39
- z-index: 100;
62
+ top: var(--sui-text-editor-toolbar-top, 0px);
63
+ z-index: var(--sui-text-editor-toolbar-z-index, 100);
40
64
  display: flex;
41
65
  flex-wrap: wrap;
42
66
  gap: 4px;
@@ -52,6 +76,9 @@
52
76
  min-height: 40px;
53
77
  background-color: var(--sui-tertiary-background-color);
54
78
  }
79
+ .wrapper :global([role=toolbar][aria-disabled=true]) {
80
+ filter: none;
81
+ }
55
82
  .wrapper :global(.sui.menu-button) {
56
83
  padding: 0 4px;
57
84
  }
@@ -124,3 +124,22 @@
124
124
  @content;
125
125
  }
126
126
  }
127
+
128
+ // The root of a text or code editor, whose sticky toolbar stays below the toolbars of the outer
129
+ // editors when nested, e.g. in an editor component. The root sets the toolbar height inline, which
130
+ // is added to the offset for the inner editors, while their z-index goes down so the outer
131
+ // toolbars stay on top of an inner toolbar being scrolled away.
132
+ @mixin nestable-editor {
133
+ // Wrapped in `&` to keep the declarations before any nested rules of the including block
134
+ & {
135
+ --sui-text-editor-toolbar-top: var(--sui-text-editor-inner-toolbar-top, 0px);
136
+ --sui-text-editor-toolbar-z-index: var(--sui-text-editor-inner-toolbar-z-index, 100);
137
+ }
138
+
139
+ & > :global(*) {
140
+ --sui-text-editor-inner-toolbar-top: calc(
141
+ var(--sui-text-editor-toolbar-top) + var(--sui-text-editor-toolbar-height)
142
+ );
143
+ --sui-text-editor-inner-toolbar-z-index: calc(var(--sui-text-editor-toolbar-z-index) - 1);
144
+ }
145
+ }
@@ -1048,6 +1048,17 @@ export type TextEditorStore = {
1048
1048
  * mode.
1049
1049
  */
1050
1050
  textArea: HTMLTextAreaElement | undefined;
1051
+ /**
1052
+ * Height of the sticky toolbar in pixels, which the toolbars of
1053
+ * nested editors are placed below. `0` while the toolbar isn’t rendered.
1054
+ */
1055
+ toolbarHeight: number;
1056
+ /**
1057
+ * Whether the focus is in an editor component within the
1058
+ * content, like a nested editor or a field of the component, rather than on the editor itself. The
1059
+ * toolbar is disabled meanwhile, as it would only apply to the editor itself.
1060
+ */
1061
+ componentHasFocus: boolean;
1051
1062
  /**
1052
1063
  * ID of the editable element currently shown: the Lexical root in the
1053
1064
  * rich text mode, or the `<textarea>` in the plain text mode.
package/dist/typedefs.js CHANGED
@@ -455,6 +455,11 @@
455
455
  * plain text editor.
456
456
  * @property {HTMLTextAreaElement | undefined} textArea The `<textarea>` element of the plain text
457
457
  * mode.
458
+ * @property {number} toolbarHeight Height of the sticky toolbar in pixels, which the toolbars of
459
+ * nested editors are placed below. `0` while the toolbar isn’t rendered.
460
+ * @property {boolean} componentHasFocus Whether the focus is in an editor component within the
461
+ * content, like a nested editor or a field of the component, rather than on the editor itself. The
462
+ * toolbar is disabled meanwhile, as it would only apply to the editor itself.
458
463
  * @property {string} controlId ID of the editable element currently shown: the Lexical root in the
459
464
  * rich text mode, or the `<textarea>` in the plain text mode.
460
465
  * @property {boolean} hasConverterError Whether there was an error while converting Markdown or
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltia/ui",
3
- "version": "0.85.0",
3
+ "version": "0.85.2",
4
4
  "description": "A collection of Svelte components and utilities for building user interfaces.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -76,7 +76,7 @@
76
76
  "@sveltejs/vite-plugin-svelte": "^7.3.1",
77
77
  "@vitest/browser-playwright": "^5.0.4",
78
78
  "@vitest/coverage-v8": "^5.0.4",
79
- "cspell": "^10.3.6",
79
+ "cspell": "^10.4.0",
80
80
  "eslint": "^9.39.5",
81
81
  "eslint-config-airbnb-extended": "^3.3.0",
82
82
  "eslint-config-prettier": "^10.1.8",
@@ -92,7 +92,7 @@
92
92
  "postcss-html": "^2.0.0",
93
93
  "prettier": "^3.9.10",
94
94
  "prettier-plugin-svelte": "^4.1.1",
95
- "rolldown": "^1.2.13",
95
+ "rolldown": "^1.2.14",
96
96
  "sass": "^1.105.1",
97
97
  "shiki": "^4.5.0",
98
98
  "stylelint": "^17.16.0",