INIT
This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
||||
<script lang="ts">
|
||||
import { boxWith } from "svelte-toolbelt";
|
||||
import type { EscapeLayerImplProps } from "./types.js";
|
||||
import { EscapeLayerState } from "./use-escape-layer.svelte.js";
|
||||
import { noop } from "../../../internal/noop.js";
|
||||
|
||||
let {
|
||||
escapeKeydownBehavior = "close",
|
||||
onEscapeKeydown = noop,
|
||||
children,
|
||||
enabled,
|
||||
ref,
|
||||
}: EscapeLayerImplProps = $props();
|
||||
|
||||
EscapeLayerState.create({
|
||||
escapeKeydownBehavior: boxWith(() => escapeKeydownBehavior),
|
||||
onEscapeKeydown: boxWith(() => onEscapeKeydown),
|
||||
enabled: boxWith(() => enabled),
|
||||
ref,
|
||||
});
|
||||
</script>
|
||||
|
||||
{@render children?.()}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import type { EscapeLayerImplProps } from "./types.js";
|
||||
declare const EscapeLayer: import("svelte").Component<EscapeLayerImplProps, {}, "">;
|
||||
type EscapeLayer = ReturnType<typeof EscapeLayer>;
|
||||
export default EscapeLayer;
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export { default as EscapeLayer } from "./escape-layer.svelte";
|
||||
export type { EscapeLayerProps } from "./types.js";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export { default as EscapeLayer } from "./escape-layer.svelte";
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import type { Snippet } from "svelte";
|
||||
import type { Box } from "svelte-toolbelt";
|
||||
export type EscapeBehaviorType = "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore";
|
||||
export type EscapeLayerProps = {
|
||||
/**
|
||||
* Callback fired when escape is pressed.
|
||||
*/
|
||||
onEscapeKeydown?: (e: KeyboardEvent) => void;
|
||||
/**
|
||||
* Escape behavior type.
|
||||
* `close`: Closes the element immediately.
|
||||
* `defer-otherwise-close`: Delegates the action to its parent component that has an
|
||||
* escape keydown handler. If no parent is found, it closes the element.
|
||||
* `defer-otherwise-ignore`: Delegates the action to the parent element. If no parent is found, nothing is done.
|
||||
* `ignore`: Prevents the element from closing and also blocks the parent element from closing in response to an escape key press.
|
||||
*
|
||||
* @defaultValue `close`
|
||||
*/
|
||||
escapeKeydownBehavior?: EscapeBehaviorType;
|
||||
};
|
||||
export type EscapeLayerImplProps = {
|
||||
/**
|
||||
* Whether the layer is enabled. Currently, we determine this with the
|
||||
* `presence` returned from the `presence` layer.
|
||||
*/
|
||||
enabled: boolean;
|
||||
children?: Snippet;
|
||||
ref: Box<HTMLElement | null>;
|
||||
} & EscapeLayerProps;
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
Generated
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
import { DOMContext, type Box, type ReadableBoxedValues } from "svelte-toolbelt";
|
||||
import type { EscapeLayerImplProps } from "./types.js";
|
||||
interface EscapeLayerStateOpts extends ReadableBoxedValues<Required<Omit<EscapeLayerImplProps, "children" | "ref">>> {
|
||||
ref: Box<HTMLElement | null>;
|
||||
}
|
||||
export declare class EscapeLayerState {
|
||||
#private;
|
||||
static create(opts: EscapeLayerStateOpts): EscapeLayerState;
|
||||
readonly opts: EscapeLayerStateOpts;
|
||||
readonly domContext: DOMContext;
|
||||
constructor(opts: EscapeLayerStateOpts);
|
||||
}
|
||||
export {};
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
import { DOMContext } from "svelte-toolbelt";
|
||||
import { watch } from "runed";
|
||||
import { on } from "svelte/events";
|
||||
import { kbd } from "../../../internal/kbd.js";
|
||||
import { noop } from "../../../internal/noop.js";
|
||||
globalThis.bitsEscapeLayers ??= new Map();
|
||||
export class EscapeLayerState {
|
||||
static create(opts) {
|
||||
return new EscapeLayerState(opts);
|
||||
}
|
||||
opts;
|
||||
domContext;
|
||||
constructor(opts) {
|
||||
this.opts = opts;
|
||||
this.domContext = new DOMContext(this.opts.ref);
|
||||
let unsubEvents = noop;
|
||||
watch(() => opts.enabled.current, (enabled) => {
|
||||
if (enabled) {
|
||||
globalThis.bitsEscapeLayers.set(this, opts.escapeKeydownBehavior);
|
||||
unsubEvents = this.#addEventListener();
|
||||
}
|
||||
return () => {
|
||||
unsubEvents();
|
||||
globalThis.bitsEscapeLayers.delete(this);
|
||||
};
|
||||
});
|
||||
}
|
||||
#addEventListener = () => {
|
||||
return on(this.domContext.getDocument(), "keydown", this.#onkeydown, { passive: false });
|
||||
};
|
||||
#onkeydown = (e) => {
|
||||
if (e.key !== kbd.ESCAPE || !isResponsibleEscapeLayer(this))
|
||||
return;
|
||||
const clonedEvent = new KeyboardEvent(e.type, e);
|
||||
e.preventDefault();
|
||||
const behaviorType = this.opts.escapeKeydownBehavior.current;
|
||||
if (behaviorType !== "close" && behaviorType !== "defer-otherwise-close")
|
||||
return;
|
||||
this.opts.onEscapeKeydown.current(clonedEvent);
|
||||
};
|
||||
}
|
||||
function isResponsibleEscapeLayer(instance) {
|
||||
const layersArr = [...globalThis.bitsEscapeLayers];
|
||||
/**
|
||||
* We first check if we can find a top layer with `close` or `ignore`.
|
||||
* If that top layer was found and matches the provided node, then the node is
|
||||
* responsible for the escape. Otherwise, we know that all layers defer so
|
||||
* the first layer is the responsible one.
|
||||
*/
|
||||
const topMostLayer = layersArr.findLast(([_, { current: behaviorType }]) => behaviorType === "close" || behaviorType === "ignore");
|
||||
if (topMostLayer)
|
||||
return topMostLayer[0] === instance;
|
||||
const [firstLayerNode] = layersArr[0];
|
||||
return firstLayerNode === instance;
|
||||
}
|
||||
Reference in New Issue
Block a user