-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
- Added a basic Checkbox component
- Updated deps - Added DataAttributes<T> type
1 parent
a8c8a78
commit f27fbe0
Showing
14 changed files
with
1,185 additions
and
547 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// MIT License | ||
|
||
// Copyright (c) 2020 Tailwind Labs | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
// https://github.com/tailwindlabs/headlessui/blob/d71fb9cd2e12f5a48617b26e6bb3db90b3e07965/packages/%40headlessui-vue/src/hooks/use-id.ts | ||
|
||
// TODO: Should this be a rune in Svelte 5? | ||
let id = 0; | ||
function generateId() { | ||
return ++id; | ||
} | ||
|
||
export function useId() { | ||
return generateId(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<script lang="ts"> | ||
import type { Component, Snippet } from "svelte"; | ||
import { useId } from "../../hooks/use-id"; | ||
type Props = { | ||
/** The element or component the checkbox should render as. */ | ||
as?: string | Component; | ||
/** Whether or not the checkbox should receive focus when first rendered. */ | ||
autofocus?: boolean; | ||
/** Whether or not the checkbox is checked. */ | ||
checked?: boolean; | ||
// TODO: defaultChecked | ||
/** Whether or not the checkbox is disabled. */ | ||
disabled?: boolean; | ||
/** | ||
* The id of the form that the checkbox belongs to. | ||
* If name is provided but form is not, the switch will add its state to the nearest ancestor form element. | ||
*/ | ||
form?: string; | ||
/** Whether or not the checkbox is indeterminate. */ | ||
indeterminate?: boolean; | ||
/** The name used when using the checkbox inside a form. */ | ||
name?: string; | ||
/** The value used when using this component inside a form, if it is checked. */ | ||
value?: string; | ||
children?: Snippet<[SnippetProps]>; | ||
}; | ||
type SnippetProps = { | ||
/** Whether or not the checkbox is in an active or pressed state. */ | ||
active?: boolean; | ||
/** Whether or not the autofocus prop was set to true. */ | ||
autofocus?: boolean; | ||
/** | ||
* Whether or not the checked state is currently changing. | ||
* When the checked state changes, changing will be true for two animation frames, | ||
* allowing you to fine-tune transitions. | ||
*/ | ||
changing?: boolean; | ||
/** Whether or not the checkbox is checked. */ | ||
checked?: boolean; | ||
/** Whether or not the checkbox is disabled. */ | ||
disabled?: boolean; | ||
/** Whether or not the checkbox is focused. */ | ||
focus?: boolean; | ||
/** Whether or not the checkbox is hovered. */ | ||
hover?: boolean; | ||
/** Whether or not the checkbox is indeterminate. */ | ||
indeterminate?: boolean; | ||
}; | ||
let { | ||
id = `headlessui-checkbox-${useId()}`, | ||
as = "span", | ||
autofocus = false, | ||
disabled = false, | ||
indeterminate = false, | ||
children, | ||
...theirProps | ||
}: Props & Record<string, any> = $props(); | ||
let ourProps = { | ||
id, | ||
autofocus, | ||
disabled, | ||
role: "checkbox", | ||
// "aria-invalid": invalid, // ? "" : undefined, | ||
// "aria-labelledby": labelledBy, | ||
// "aria-describedby": describedBy, | ||
}; | ||
let snippetProps: SnippetProps = { | ||
autofocus, | ||
disabled, | ||
focus: false, | ||
hover: false, | ||
}; | ||
// TODO: Utility function to create this | ||
let dataAttributes: DataAttributes<SnippetProps> = { | ||
"data-autofocus": autofocus, | ||
"data-disabled": disabled, | ||
"data-focus": false, | ||
"data-hover": false, | ||
}; | ||
</script> | ||
|
||
{#if typeof as === "string"} | ||
<svelte:element this={as} {...theirProps} {...ourProps} {...dataAttributes}> | ||
{@render children?.(snippetProps)} | ||
</svelte:element> | ||
{:else} | ||
{@const Component = as} | ||
<Component {...theirProps} {...ourProps} {...dataAttributes}> | ||
{@render children?.(snippetProps)} | ||
</Component> | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { render, screen } from "@testing-library/svelte"; | ||
import { getCheckbox } from "../../test-utils/accessibility-assertions"; | ||
import type { SvelteComponent } from "svelte"; | ||
|
||
function sveltify(input: string): Promise<typeof SvelteComponent> { | ||
throw new Error("TODO"); | ||
} | ||
|
||
// TODO: Manually unrolled test-utils/scenarios.ts commonRenderingScenarios | ||
|
||
describe("Rendering", () => { | ||
it("should render a checkbox", async () => { | ||
const component = await sveltify(` | ||
<script> | ||
import Checkbox from "$lib/checkbox/Checkbox.svelte"; | ||
</script> | ||
<Checkbox /> | ||
`); | ||
render(component); | ||
|
||
expect(getCheckbox()).toBeInTheDocument(); | ||
}); | ||
|
||
it("should have an `id` attached", async () => { | ||
const component = await sveltify(` | ||
<script> | ||
import Checkbox from "$lib/checkbox/Checkbox.svelte"; | ||
</script> | ||
<Checkbox /> | ||
`); | ||
render(component); | ||
|
||
expect(getCheckbox()).toHaveAttribute("id"); | ||
}); | ||
|
||
it("should be possible to override the `id`", async () => { | ||
const component = await sveltify(` | ||
<script> | ||
import Checkbox from "$lib/checkbox/Checkbox.svelte"; | ||
</script> | ||
<Checkbox id="foo" /> | ||
`); | ||
render(component); | ||
|
||
expect(getCheckbox()).toHaveAttribute("id", "foo"); | ||
}); | ||
}); | ||
|
||
describe.skip("commonControlScenarios", () => {}); | ||
describe.skip("commonFormScenarios", () => {}); | ||
|
||
// describe.each([ | ||
// [ | ||
// 'Uncontrolled', | ||
// function Example(props: CheckboxProps) { | ||
// return <Checkbox {...props} /> | ||
// }, | ||
// ], | ||
// [ | ||
// 'Controlled', | ||
// function Example(props: CheckboxProps) { | ||
// let [checked, setChecked] = useState(false) | ||
// return <Checkbox checked={checked} onChange={setChecked} {...props} /> | ||
// }, | ||
// ], | ||
// ])('Keyboard interactions (%s)', (_, Example) => { | ||
// describe('`Space` key', () => { | ||
// it( | ||
// 'should be possible to toggle a checkbox', | ||
// suppressConsoleLogs(async () => { | ||
// render(<Example />) | ||
|
||
// assertCheckbox({ state: CheckboxState.Unchecked }) | ||
|
||
// await focus(getCheckbox()) | ||
// await press(Keys.Space) | ||
|
||
// assertCheckbox({ state: CheckboxState.Checked }) | ||
|
||
// await press(Keys.Space) | ||
|
||
// assertCheckbox({ state: CheckboxState.Unchecked }) | ||
// }) | ||
// ) | ||
// }) | ||
// }) | ||
|
||
// describe.each([ | ||
// [ | ||
// 'Uncontrolled', | ||
// function Example(props: CheckboxProps) { | ||
// return <Checkbox {...props} /> | ||
// }, | ||
// ], | ||
// [ | ||
// 'Controlled', | ||
// function Example(props: CheckboxProps) { | ||
// let [checked, setChecked] = useState(false) | ||
// return <Checkbox checked={checked} onChange={setChecked} {...props} /> | ||
// }, | ||
// ], | ||
// ])('Mouse interactions (%s)', (_, Example) => { | ||
// it( | ||
// 'should be possible to toggle a checkbox by clicking it', | ||
// suppressConsoleLogs(async () => { | ||
// render(<Example />) | ||
|
||
// assertCheckbox({ state: CheckboxState.Unchecked }) | ||
|
||
// await click(getCheckbox()) | ||
|
||
// assertCheckbox({ state: CheckboxState.Checked }) | ||
|
||
// await click(getCheckbox()) | ||
|
||
// assertCheckbox({ state: CheckboxState.Unchecked }) | ||
// }) | ||
// ) | ||
// }) | ||
|
||
// describe('Form submissions', () => { | ||
// it('should be possible to use in an uncontrolled way', async () => { | ||
// let handleSubmission = jest.fn() | ||
|
||
// render( | ||
// <form | ||
// onSubmit={(e) => { | ||
// e.preventDefault() | ||
// handleSubmission(Object.fromEntries(new FormData(e.target as HTMLFormElement))) | ||
// }} | ||
// > | ||
// <Checkbox name="notifications" /> | ||
// </form> | ||
// ) | ||
|
||
// let checkbox = document.querySelector('[id^="headlessui-checkbox-"]') as HTMLInputElement | ||
|
||
// // Focus the checkbox | ||
// await focus(checkbox) | ||
|
||
// // Submit | ||
// await press(Keys.Enter) | ||
|
||
// // No values | ||
// expect(handleSubmission).toHaveBeenLastCalledWith({}) | ||
|
||
// // Toggle | ||
// await click(checkbox) | ||
|
||
// // Submit | ||
// await press(Keys.Enter) | ||
|
||
// // Notifications should be on | ||
// expect(handleSubmission).toHaveBeenLastCalledWith({ notifications: 'on' }) | ||
|
||
// // Toggle | ||
// await click(checkbox) | ||
|
||
// // Submit | ||
// await press(Keys.Enter) | ||
|
||
// // Notifications should be off (or in this case, non-existent) | ||
// expect(handleSubmission).toHaveBeenLastCalledWith({}) | ||
// }) | ||
// }) |
Oops, something went wrong.