@melt-ui/svelte
Advanced tools
Comparing version 0.37.3 to 0.37.4
@@ -85,15 +85,3 @@ import { addMeltEventListener, builder, createElHelpers, derivedVisible, effect, generateId, getPortalDestination, isBrowser, isHTMLElement, kbd, noop, omit, overridable, removeScroll, styleToString, toWritableStores, } from '../../internal/helpers/index.js'; | ||
focusTrap: $disableFocusTrap ? null : undefined, | ||
clickOutside: $closeOnOutsideClick | ||
? { | ||
handler: (e) => { | ||
if (!isHTMLElement(e.target)) | ||
return; | ||
if ($activeTrigger.contains(e.target)) | ||
return; | ||
if (e.target === $activeTrigger) | ||
return; | ||
handleClose(); | ||
}, | ||
} | ||
: null, | ||
clickOutside: $closeOnOutsideClick ? undefined : null, | ||
escapeKeydown: $closeOnEscape | ||
@@ -139,2 +127,3 @@ ? { | ||
const unsub = executeCallbacks(addMeltEventListener(node, 'click', () => { | ||
activeTrigger.set(node); | ||
toggleOpen(); | ||
@@ -145,2 +134,3 @@ }), addMeltEventListener(node, 'keydown', (e) => { | ||
e.preventDefault(); | ||
activeTrigger.set(node); | ||
toggleOpen(); | ||
@@ -147,0 +137,0 @@ })); |
@@ -1,2 +0,3 @@ | ||
import { addMeltEventListener, builder, createElHelpers, executeCallbacks, getDirectionalKeys, getElemDirection, isHTMLElement, kbd, omit, overridable, toWritableStores, } from '../../internal/helpers/index.js'; | ||
import { addEventListener, addMeltEventListener, builder, createElHelpers, effect, executeCallbacks, getDirectionalKeys, getElemDirection, isHTMLElement, kbd, omit, overridable, toWritableStores, } from '../../internal/helpers/index.js'; | ||
import { onMount } from 'svelte'; | ||
import { derived, get, writable } from 'svelte/store'; | ||
@@ -18,2 +19,34 @@ const defaults = { | ||
const value = overridable(valueWritable, withDefaults?.onValueChange); | ||
/** Lifecycle & Effects */ | ||
const focusedHistory = { | ||
prev: null, | ||
curr: null, | ||
}; | ||
onMount(() => { | ||
return addEventListener(document, 'focus', (e) => { | ||
const focusedItem = e.target; | ||
if (!isHTMLElement(focusedItem)) | ||
return; | ||
focusedHistory.prev = focusedHistory.curr; | ||
focusedHistory.curr = focusedItem; | ||
}); | ||
}); | ||
let hasActiveTabIndex = false; | ||
effect(value, ($value) => { | ||
if ($value === undefined) { | ||
hasActiveTabIndex = false; | ||
} | ||
else { | ||
hasActiveTabIndex = true; | ||
} | ||
}); | ||
/* Helpers */ | ||
const selectItem = (item) => { | ||
const disabled = item.dataset.disabled === 'true'; | ||
const itemValue = item.dataset.value; | ||
if (disabled || itemValue === undefined) | ||
return; | ||
value.set(itemValue); | ||
}; | ||
/** Elements */ | ||
const root = builder(name(), { | ||
@@ -37,2 +70,4 @@ stores: [required, orientation], | ||
const checked = $value === itemValue; | ||
const tabindex = !hasActiveTabIndex ? 0 : checked ? 0 : -1; | ||
hasActiveTabIndex = true; | ||
return { | ||
@@ -47,3 +82,3 @@ disabled, | ||
role: 'radio', | ||
tabindex: $value === null ? 0 : checked ? 0 : -1, | ||
tabindex, | ||
}; | ||
@@ -54,13 +89,3 @@ }; | ||
const unsub = executeCallbacks(addMeltEventListener(node, 'click', () => { | ||
const disabled = node.dataset.disabled === 'true'; | ||
const itemValue = node.dataset.value; | ||
if (disabled || itemValue === undefined) | ||
return; | ||
value.set(itemValue); | ||
}), addMeltEventListener(node, 'focus', () => { | ||
const disabled = node.dataset.disabled === 'true'; | ||
const itemValue = node.dataset.value; | ||
if (disabled || itemValue === undefined) | ||
return; | ||
value.set(itemValue); | ||
selectItem(node); | ||
}), addMeltEventListener(node, 'keydown', (e) => { | ||
@@ -78,2 +103,3 @@ const el = e.currentTarget; | ||
const $loop = get(loop); | ||
let itemToFocus = null; | ||
if (e.key === nextKey) { | ||
@@ -83,6 +109,6 @@ e.preventDefault(); | ||
if (nextIndex >= items.length && $loop) { | ||
items[0].focus(); | ||
itemToFocus = items[0]; | ||
} | ||
else { | ||
items[nextIndex].focus(); | ||
itemToFocus = items[nextIndex]; | ||
} | ||
@@ -94,6 +120,6 @@ } | ||
if (prevIndex < 0 && $loop) { | ||
items[items.length - 1].focus(); | ||
itemToFocus = items[items.length - 1]; | ||
} | ||
else { | ||
items[prevIndex].focus(); | ||
itemToFocus = items[prevIndex]; | ||
} | ||
@@ -103,8 +129,12 @@ } | ||
e.preventDefault(); | ||
items[0].focus(); | ||
itemToFocus = items[0]; | ||
} | ||
else if (e.key === kbd.END) { | ||
e.preventDefault(); | ||
items[items.length - 1].focus(); | ||
itemToFocus = items[items.length - 1]; | ||
} | ||
if (itemToFocus) { | ||
itemToFocus.focus(); | ||
selectItem(itemToFocus); | ||
} | ||
})); | ||
@@ -111,0 +141,0 @@ return { |
{ | ||
"name": "@melt-ui/svelte", | ||
"version": "0.37.3", | ||
"version": "0.37.4", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "exports": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
594008
13764