Skip to content

Commit bea6356

Browse files
committed
Focus when selection updates
1 parent 6661448 commit bea6356

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "solid-nest",
3-
"version": "0.6.1",
3+
"version": "0.6.2",
44
"description": "Nested block UI component, supporting drag-and-drop, multi-select and smooth animations",
55
"license": "MIT",
66
"author": "Rafferty97",

src/BlockTree.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Accessor, Component, createMemo, For, JSX, onCleanup, onMount, Show } from 'solid-js'
1+
import { Accessor, Component, createEffect, createMemo, For, JSX, onCleanup, onMount, Show } from 'solid-js'
22
import { Dynamic } from 'solid-js/web'
33
import { BlockItem, Item, ItemId } from './Item'
44
import {
@@ -262,7 +262,6 @@ export function BlockTree<K, T>(props: BlockTreeProps<K, T>) {
262262
const select = () => {
263263
const { mode, keys } = nextSelection
264264
props.onSelectionChange?.({ kind: 'blocks', key: item.key, mode, blocks: keys })
265-
focusElement.focus({ preventScroll: true })
266265
}
267266

268267
if (nextSelection.onClick) {
@@ -287,6 +286,18 @@ export function BlockTree<K, T>(props: BlockTreeProps<K, T>) {
287286
}
288287
}
289288

289+
createEffect(() => {
290+
const selection = props.selection
291+
if (!selection) return
292+
293+
const hasSelection = (selection.blocks?.length ?? 0) > 0 || selection.place != null
294+
const hasFocus = document.activeElement === focusElement
295+
296+
if (hasSelection && !hasFocus) {
297+
focusElement.focus({ preventScroll: true })
298+
}
299+
})
300+
290301
const renderItem = (
291302
item: Item<K, T>,
292303
tree: Accessor<VirtualTree<K, T>>,

0 commit comments

Comments
 (0)