$api?. => $api. when possible

Now that this stuff has all been cleaned up with useXContext functions
This commit is contained in:
Ryan Gossiaux
2021-12-18 23:56:37 -08:00
parent 70910c05b0
commit 305dc1543f
13 changed files with 50 additions and 50 deletions

View File

@@ -142,7 +142,7 @@
if (containers.size !== 1) return; if (containers.size !== 1) return;
if (contains(containers, target)) return; if (contains(containers, target)) return;
$api?.close(); $api.close();
await tick(); await tick();
target?.focus(); target?.focus();
} }
@@ -154,7 +154,7 @@
if (containers.size > 1) return; // 1 is myself, otherwise other elements in the Stack if (containers.size > 1) return; // 1 is myself, otherwise other elements in the Stack
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
$api?.close(); $api.close();
} }
let mounted = false; let mounted = false;
@@ -202,7 +202,7 @@
entry.boundingClientRect.width === 0 && entry.boundingClientRect.width === 0 &&
entry.boundingClientRect.height === 0 entry.boundingClientRect.height === 0
) { ) {
$api?.close(); $api.close();
} }
} }
}); });

View File

@@ -7,7 +7,7 @@
if (event.target !== event.currentTarget) return; if (event.target !== event.currentTarget) return;
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
$api?.close(); $api.close();
} }
$: propsWeControl = { $: propsWeControl = {
id, id,
@@ -16,5 +16,5 @@
</script> </script>
<div {...{ ...$$restProps, ...propsWeControl }} on:click={handleClick}> <div {...{ ...$$restProps, ...propsWeControl }} on:click={handleClick}>
<slot open={$api?.dialogState === DialogStates.Open} /> <slot open={$api.dialogState === DialogStates.Open} />
</div> </div>

View File

@@ -103,5 +103,5 @@
</script> </script>
<div {...$$restProps}> <div {...$$restProps}>
<slot open={disclosureState === DisclosureStates.Open} close={$api?.close} /> <slot open={disclosureState === DisclosureStates.Open} close={$api.close} />
</div> </div>

View File

@@ -8,11 +8,11 @@
const panelContext = usePanelContext(); const panelContext = usePanelContext();
const id = `headlessui-disclosure-button-${useId()}`; const id = `headlessui-disclosure-button-${useId()}`;
$: buttonStore = $api?.buttonStore; $: buttonStore = $api.buttonStore;
$: panelStore = $api?.panelStore; $: panelStore = $api.panelStore;
$: isWithinPanel = $: isWithinPanel =
panelContext === null ? false : panelContext === $api?.panelId; panelContext === null ? false : panelContext === $api.panelId;
function handleClick() { function handleClick() {
if (disabled) return; if (disabled) return;
@@ -68,7 +68,7 @@
"aria-expanded": disabled "aria-expanded": disabled
? undefined ? undefined
: $api.disclosureState === DisclosureStates.Open, : $api.disclosureState === DisclosureStates.Open,
"aria-controls": $panelStore ? $api?.panelId : undefined, "aria-controls": $panelStore ? $api.panelId : undefined,
disabled: disabled ? true : undefined, disabled: disabled ? true : undefined,
}; };
</script> </script>
@@ -80,8 +80,8 @@
on:keydown={handleKeyDown} on:keydown={handleKeyDown}
> >
<slot <slot
open={$api?.disclosureState === DisclosureStates.Open} open={$api.disclosureState === DisclosureStates.Open}
close={$api?.close} close={$api.close}
/> />
</button> </button>
{:else} {:else}
@@ -93,8 +93,8 @@
on:keyup={handleKeyUp} on:keyup={handleKeyUp}
> >
<slot <slot
open={$api?.disclosureState === DisclosureStates.Open} open={$api.disclosureState === DisclosureStates.Open}
close={$api?.close} close={$api.close}
/> />
</button> </button>
{/if} {/if}

View File

@@ -11,17 +11,17 @@
import { useDisclosureContext, DisclosureStates } from "./Disclosure.svelte"; import { useDisclosureContext, DisclosureStates } from "./Disclosure.svelte";
import { State, useOpenClosed } from "$lib/internal/open-closed"; import { State, useOpenClosed } from "$lib/internal/open-closed";
const api = useDisclosureContext("DisclosureButton"); const api = useDisclosureContext("DisclosureButton");
$: id = $api?.panelId; $: id = $api.panelId;
let openClosedState = useOpenClosed(); let openClosedState = useOpenClosed();
setContext(DISCLOSURE_PANEL_CONTEXT_NAME, id); setContext(DISCLOSURE_PANEL_CONTEXT_NAME, id);
$: panelStore = $api?.panelStore; $: panelStore = $api.panelStore;
$: visible = $: visible =
$openClosedState !== null $openClosedState !== null
? $openClosedState === State.Open ? $openClosedState === State.Open
: $api?.disclosureState === DisclosureStates.Open; : $api.disclosureState === DisclosureStates.Open;
$: propsWeControl = { id }; $: propsWeControl = { id };
</script> </script>
@@ -29,8 +29,8 @@
{#if visible} {#if visible}
<div {...{ ...$$restProps, ...propsWeControl }} bind:this={$panelStore}> <div {...{ ...$$restProps, ...propsWeControl }} bind:this={$panelStore}>
<slot <slot
open={$api?.disclosureState === DisclosureStates.Open} open={$api.disclosureState === DisclosureStates.Open}
close={$api?.close} close={$api.close}
/> />
</div> </div>
{/if} {/if}

View File

@@ -65,9 +65,9 @@
"aria-controls": $optionsRef?.id, "aria-controls": $optionsRef?.id,
"aria-expanded": $api.disabled "aria-expanded": $api.disabled
? undefined ? undefined
: $api?.listboxState === ListboxStates.Open, : $api.listboxState === ListboxStates.Open,
"aria-labelledby": $labelRef ? [$labelRef?.id, id].join(" ") : undefined, "aria-labelledby": $labelRef ? [$labelRef?.id, id].join(" ") : undefined,
disabled: $api?.disabled === true ? true : undefined, disabled: $api.disabled === true ? true : undefined,
}; };
</script> </script>

View File

@@ -11,11 +11,11 @@
let buttonRef = $api.buttonRef; let buttonRef = $api.buttonRef;
$: active = $: active =
$api?.activeOptionIndex !== null $api.activeOptionIndex !== null
? $api?.options[$api.activeOptionIndex].id === id ? $api.options[$api.activeOptionIndex].id === id
: false; : false;
$: selected = $api?.value === value; $: selected = $api.value === value;
$: dataRef = { $: dataRef = {
disabled, disabled,
value, value,
@@ -33,7 +33,7 @@
onMount(() => $api.registerOption(id, dataRef)); onMount(() => $api.registerOption(id, dataRef));
onDestroy(() => $api.unregisterOption(id)); onDestroy(() => $api.unregisterOption(id));
let oldState = $api?.listboxState; let oldState = $api.listboxState;
let oldSelected = selected; let oldSelected = selected;
let oldActive = active; let oldActive = active;
async function updateFocus( async function updateFocus(
@@ -45,7 +45,7 @@
await tick(); await tick();
if (newState !== oldState || newSelected !== oldSelected) { if (newState !== oldState || newSelected !== oldSelected) {
if (newState === ListboxStates.Open && newSelected) { if (newState === ListboxStates.Open && newSelected) {
$api?.goToOption(Focus.Specific, id); $api.goToOption(Focus.Specific, id);
} }
} }
if (newState !== oldState || newActive !== oldActive) { if (newState !== oldState || newActive !== oldActive) {
@@ -57,7 +57,7 @@
oldSelected = newSelected; oldSelected = newSelected;
oldActive = newActive; oldActive = newActive;
} }
$: updateFocus($api?.listboxState, selected, active); $: updateFocus($api.listboxState, selected, active);
async function handleClick(event: MouseEvent) { async function handleClick(event: MouseEvent) {
if (disabled) return event.preventDefault(); if (disabled) return event.preventDefault();

View File

@@ -91,11 +91,11 @@
$: propsWeControl = { $: propsWeControl = {
"aria-activedescendant": "aria-activedescendant":
$api?.activeOptionIndex === null $api.activeOptionIndex === null
? undefined ? undefined
: $api?.options[$api.activeOptionIndex]?.id, : $api.options[$api.activeOptionIndex]?.id,
"aria-labelledby": $labelRef?.id ?? $buttonRef?.id, "aria-labelledby": $labelRef?.id ?? $buttonRef?.id,
"aria-orientation": $api?.orientation, "aria-orientation": $api.orientation,
id, id,
role: "listbox", role: "listbox",
tabIndex: 0, tabIndex: 0,
@@ -115,6 +115,6 @@
{...$$restProps} {...$$restProps}
{...propsWeControl} {...propsWeControl}
> >
<slot open={$api?.listboxState === ListboxStates.Open} /> <slot open={$api.listboxState === ListboxStates.Open} />
</ul> </ul>
{/if} {/if}

View File

@@ -10,8 +10,8 @@
const api = useMenuContext("MenuButton"); const api = useMenuContext("MenuButton");
const id = `headlessui-menu-button-${useId()}`; const id = `headlessui-menu-button-${useId()}`;
$: buttonStore = $api?.buttonStore; $: buttonStore = $api.buttonStore;
$: itemsStore = $api?.itemsStore; $: itemsStore = $api.itemsStore;
async function handleKeyDown(event: KeyboardEvent) { async function handleKeyDown(event: KeyboardEvent) {
switch (event.key) { switch (event.key) {
// Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-13 // Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-13
@@ -80,5 +80,5 @@
on:keydown={handleKeyDown} on:keydown={handleKeyDown}
on:keyup={handleKeyUp} on:keyup={handleKeyUp}
> >
<slot open={$api?.menuState === MenuStates.Open} /> <slot open={$api.menuState === MenuStates.Open} />
</button> </button>

View File

@@ -10,11 +10,11 @@
const id = `headlessui-menu-item-${useId()}`; const id = `headlessui-menu-item-${useId()}`;
$: active = $: active =
$api?.activeItemIndex !== null $api.activeItemIndex !== null
? $api?.items[$api?.activeItemIndex].id === id ? $api.items[$api.activeItemIndex].id === id
: false; : false;
$: buttonStore = $api?.buttonStore; $: buttonStore = $api.buttonStore;
let elementRef: HTMLDivElement | undefined; let elementRef: HTMLDivElement | undefined;
$: textValue = elementRef?.textContent?.toLowerCase().trim(); $: textValue = elementRef?.textContent?.toLowerCase().trim();
@@ -22,7 +22,7 @@
onMount(async () => { onMount(async () => {
await tick(); await tick();
$api?.registerItem(id, data); $api.registerItem(id, data);
}); });
onDestroy(() => { onDestroy(() => {

View File

@@ -12,8 +12,8 @@
const id = `headlessui-menu-items-${useId()}`; const id = `headlessui-menu-items-${useId()}`;
let searchDebounce: ReturnType<typeof setTimeout> | null = null; let searchDebounce: ReturnType<typeof setTimeout> | null = null;
$: buttonStore = $api?.buttonStore; $: buttonStore = $api.buttonStore;
$: itemsStore = $api?.itemsStore; $: itemsStore = $api.itemsStore;
let openClosedState = useOpenClosed(); let openClosedState = useOpenClosed();
@@ -24,7 +24,7 @@
$: treeWalker({ $: treeWalker({
container: $itemsStore, container: $itemsStore,
enabled: $api?.menuState === MenuStates.Open, enabled: $api.menuState === MenuStates.Open,
accept(node) { accept(node) {
if (node.getAttribute("role") === "menuitem") if (node.getAttribute("role") === "menuitem")
return NodeFilter.FILTER_REJECT; return NodeFilter.FILTER_REJECT;

View File

@@ -20,15 +20,15 @@
let state = OptionState.Empty; let state = OptionState.Empty;
function updateOption(option: Option) { function updateOption(option: Option) {
$api?.unregisterOption(option.id); $api.unregisterOption(option.id);
$api?.registerOption(option); $api.registerOption(option);
} }
$: updateOption({ id, element: optionRef, propsRef }); $: updateOption({ id, element: optionRef, propsRef });
onDestroy(() => $api?.unregisterOption(id)); onDestroy(() => $api.unregisterOption(id));
$: isFirstOption = $api?.firstOption?.id === id; $: isFirstOption = $api.firstOption?.id === id;
$: isDisabled = $api?.disabled || disabled; $: isDisabled = $api.disabled || disabled;
$: checked = $api?.value === value; $: checked = $api.value === value;
$: tabIndex = (() => { $: tabIndex = (() => {
if (isDisabled) return -1; if (isDisabled) return -1;

View File

@@ -22,13 +22,13 @@
$: selected = myIndex === $api.selectedIndex; $: selected = myIndex === $api.selectedIndex;
function handleKeyDown(event: KeyboardEvent) { function handleKeyDown(event: KeyboardEvent) {
let list = $api?.tabs.filter(Boolean) as HTMLElement[]; let list = $api.tabs.filter(Boolean) as HTMLElement[];
if (event.key === Keys.Space || event.key === Keys.Enter) { if (event.key === Keys.Space || event.key === Keys.Enter) {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
$api?.setSelectedIndex(myIndex); $api.setSelectedIndex(myIndex);
return; return;
} }
@@ -74,7 +74,7 @@
if (disabled) return; if (disabled) return;
tabRef?.focus(); tabRef?.focus();
$api?.setSelectedIndex(myIndex); $api.setSelectedIndex(myIndex);
} }
$: propsWeControl = { $: propsWeControl = {