Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve TypeScript support + JSDoc #81

Merged
merged 16 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 27 additions & 6 deletions src/lib/components/dialog/Dialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@
}
return context;
}

type TDialogProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp, "div"> & {
/** Whether the `Dialog` is open */
open?: boolean;
/** The element that should receive focus when the Dialog is first opened */
initialFocus?: HTMLElement | null;
/** Whether the element should ignore the internally managed open/closed state */
static?: boolean;
/** Whether the element should be unmounted, instead of just hidden, based on the open/closed state */
unmount?: boolean;
};
</script>

<script lang="ts">
Expand All @@ -60,20 +74,27 @@
import { get_current_component } from "svelte/internal";
import type { SupportedAs } from "$lib/internal/elements";
import type { HTMLActionArray } from "$lib/hooks/use-actions";
import Render, { Features } from "$lib/utils/Render.svelte";
const forwardEvents = forwardEventsBuilder(get_current_component(), [
"close",
]);
import Render from "$lib/utils/Render.svelte";
import { Features, type TPassThroughProps } from "$lib/types";

/***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TDialogProps<typeof slotProps, TAsProp>;

export let as: SupportedAs = "div";
export let use: HTMLActionArray = [];

export let open: Boolean | undefined = undefined;
export let open: boolean | undefined = undefined;
export let initialFocus: HTMLElement | null = null;

/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component(), [
"close",
]);
const dispatch = createEventDispatcher<{
close: boolean;
}>();

/***** Component *****/
let containers: Set<HTMLElement> = new Set();
let openClosedState = useOpenClosed();

Expand Down
18 changes: 17 additions & 1 deletion src/lib/components/dialog/DialogOverlay.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<script lang="ts" context="module">
type TDialogOverlayProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp, "div"> & {};
</script>

<script lang="ts">
import { DialogStates, useDialogContext } from "./Dialog.svelte";
import { useId } from "$lib/hooks/use-id";
Expand All @@ -6,11 +13,20 @@
import type { SupportedAs } from "$lib/internal/elements";
import type { HTMLActionArray } from "$lib/hooks/use-actions";
import Render from "$lib/utils/Render.svelte";
const forwardEvents = forwardEventsBuilder(get_current_component());
import type { TPassThroughProps } from "$lib/types";

/***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TDialogOverlayProps<typeof slotProps, TAsProp>;

export let as: SupportedAs = "div";
export let use: HTMLActionArray = [];

/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component());

/***** Component *****/

let api = useDialogContext("DialogOverlay");
let id = `headlessui-dialog-overlay-${useId()}`;

Expand Down
18 changes: 17 additions & 1 deletion src/lib/components/dialog/DialogTitle.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<script lang="ts" context="module">
type TDialogTitleProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp, "h2"> & {};
</script>

<script lang="ts">
import { DialogStates, useDialogContext } from "./Dialog.svelte";
import { useId } from "$lib/hooks/use-id";
Expand All @@ -7,10 +14,19 @@
import type { SupportedAs } from "$lib/internal/elements";
import type { HTMLActionArray } from "$lib/hooks/use-actions";
import Render from "$lib/utils/Render.svelte";
const forwardEvents = forwardEventsBuilder(get_current_component());
import type { TPassThroughProps } from "$lib/types";

/***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TDialogTitleProps<typeof slotProps, TAsProp>;

export let as: SupportedAs = "h2";
export let use: HTMLActionArray = [];

/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component());

/***** Component *****/
let api = useDialogContext("DialogTitle");
let id = `headlessui-dialog-title-${useId()}`;

Expand Down
20 changes: 18 additions & 2 deletions src/lib/components/disclosure/Disclosure.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@

return context;
}

type TDisclosureProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp, "div"> & {
/** Whether the `Disclosure` should be open by default */
defaultOpen?: boolean;
};
</script>

<script lang="ts">
Expand All @@ -51,12 +59,20 @@
import type { SupportedAs } from "$lib/internal/elements";
import type { HTMLActionArray } from "$lib/hooks/use-actions";
import Render from "$lib/utils/Render.svelte";
const forwardEvents = forwardEventsBuilder(get_current_component());
import type { TPassThroughProps } from "$lib/types";

/***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TDisclosureProps<typeof slotProps, TAsProp>;

export let as: SupportedAs = "div";
export let use: HTMLActionArray = [];

export let defaultOpen = false;

/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component());

/***** Component *****/
let buttonId = `headlessui-disclosure-button-${useId()}`;
let panelId = `headlessui-disclosure-panel-${useId()}`;

Expand Down
21 changes: 19 additions & 2 deletions src/lib/components/disclosure/DisclosureButton.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
<script lang="ts" context="module">
type TDisclosureButtonProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp, "button"> & {
disabled?: boolean;
};
</script>

<script lang="ts">
import { useDisclosureContext, DisclosureStates } from "./Disclosure.svelte";
import { usePanelContext } from "./DisclosurePanel.svelte";
Expand All @@ -9,12 +18,20 @@
import Render from "$lib/utils/Render.svelte";
import { writable } from "svelte/store";
import { resolveButtonType } from "$lib/utils/resolve-button-type";
const forwardEvents = forwardEventsBuilder(get_current_component());
import type { TPassThroughProps } from "$lib/types";

/***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TDisclosureButtonProps<typeof slotProps, TAsProp>;

export let as: SupportedAs = "button";
export let use: HTMLActionArray = [];

export let disabled = false;

/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component());

/***** Component *****/
const api = useDisclosureContext("DisclosureButton");
const panelContext = usePanelContext();

Expand Down
21 changes: 19 additions & 2 deletions src/lib/components/disclosure/DisclosurePanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
export function usePanelContext(): string | undefined {
return getContext(DISCLOSURE_PANEL_CONTEXT_NAME);
}
type TDisclosurePanelProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp, "div"> & {
/** Whether the element should ignore the internally managed open/closed state */
static?: boolean;
/** Whether the element should be unmounted, instead of just hidden, based on the open/closed state */
unmount?: boolean;
};
</script>

<script lang="ts">
Expand All @@ -14,12 +23,20 @@
import { get_current_component } from "svelte/internal";
import type { SupportedAs } from "$lib/internal/elements";
import type { HTMLActionArray } from "$lib/hooks/use-actions";
import Render, { Features } from "$lib/utils/Render.svelte";
const forwardEvents = forwardEventsBuilder(get_current_component());
import Render from "$lib/utils/Render.svelte";
import { Features, type TPassThroughProps } from "$lib/types";

/***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TDisclosurePanelProps<typeof slotProps, TAsProp>;

export let as: SupportedAs = "div";
export let use: HTMLActionArray = [];

/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component());

/***** Component *****/
const api = useDisclosureContext("DisclosurePanel");
let openClosedState = useOpenClosed();

Expand Down
35 changes: 28 additions & 7 deletions src/lib/components/listbox/Listbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@

return context;
}

type TListboxProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp, "div"> & {
/** Whether the entire `Listbox` and its children should be disabled */
disabled?: boolean;
/** Whether the entire `Listbox` should be oriented horizontally instead of vertically */
horizontal?: boolean;
/** The selected value */
value?: StateDefinition["value"];
};
</script>

<script lang="ts">
Expand All @@ -67,23 +79,32 @@
import type { SupportedAs } from "$lib/internal/elements";
import type { HTMLActionArray } from "$lib/hooks/use-actions";
import Render from "$lib/utils/Render.svelte";
const forwardEvents = forwardEventsBuilder(get_current_component(), [
"change",
]);
import type { TPassThroughProps } from "$lib/types";

/***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TListboxProps<typeof slotProps, TAsProp>;

export let as: SupportedAs = "div";
export let use: HTMLActionArray = [];

export let disabled = false;
export let horizontal = false;
export let value: StateDefinition["value"];
$: orientation = (
horizontal ? "horizontal" : "vertical"
) as StateDefinition["orientation"];

/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component(), [
"change",
]);

const dispatch = createEventDispatcher<{
change: any;
}>();

/***** Component *****/
$: orientation = (
horizontal ? "horizontal" : "vertical"
) as StateDefinition["orientation"];

let listboxState: StateDefinition["listboxState"] = ListboxStates.Closed;
let labelRef: StateDefinition["labelRef"] = writable(null);
let buttonRef: StateDefinition["buttonRef"] = writable(null);
Expand Down
17 changes: 16 additions & 1 deletion src/lib/components/listbox/ListboxButton.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<script lang="ts" context="module">
type TListboxButtonProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp, "button"> & {};
</script>

<script lang="ts">
import { tick } from "svelte";
import { ListboxStates, useListboxContext } from "./Listbox.svelte";
Expand All @@ -10,11 +17,19 @@
import { get_current_component } from "svelte/internal";
import Render from "$lib/utils/Render.svelte";
import { resolveButtonType } from "$lib/utils/resolve-button-type";
import type { TPassThroughProps } from "$lib/types";

/***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TListboxButtonProps<typeof slotProps, TAsProp>;

const forwardEvents = forwardEventsBuilder(get_current_component());
export let as: SupportedAs = "button";
export let use: HTMLActionArray = [];

/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component());

/***** Component *****/
let api = useListboxContext("ListboxButton");
let id = `headlessui-listbox-button-${useId()}`;
let buttonRef = $api.buttonRef;
Expand Down
17 changes: 16 additions & 1 deletion src/lib/components/listbox/ListboxLabel.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<script lang="ts" context="module">
type TListboxLabelProps<
TSlotProps extends {},
TAsProp extends SupportedAs
> = TPassThroughProps<TSlotProps, TAsProp, "label"> & {};
</script>

<script lang="ts">
import { ListboxStates, useListboxContext } from "./Listbox.svelte";
import { useId } from "$lib/hooks/use-id";
Expand All @@ -6,11 +13,19 @@
import type { SupportedAs } from "$lib/internal/elements";
import type { HTMLActionArray } from "$lib/hooks/use-actions";
import { get_current_component } from "svelte/internal";
import type { TPassThroughProps } from "$lib/types";

/***** Props *****/
type TAsProp = $$Generic<SupportedAs>;
type $$Props = TListboxLabelProps<typeof slotProps, TAsProp>;

const forwardEvents = forwardEventsBuilder(get_current_component());
export let as: SupportedAs = "label";
export let use: HTMLActionArray = [];

/***** Events *****/
const forwardEvents = forwardEventsBuilder(get_current_component());

/***** Component *****/
let id = `headlessui-listbox-label-${useId()}`;
let api = useListboxContext("ListboxLabel");

Expand Down
Loading