-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR improves the current checkbox docs and fixes two related issues: - The `Checkbox` component now accepts `string[]` as the `value` prop. This is to conform with the native input prop type. If a string array is passed, it will be stringified, just like in the native input element. - Fixed the `clickOnEnter` prop on `Checkbox` not working when rendering the component as a native input element.
- Loading branch information
Showing
25 changed files
with
394 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@ariakit/react-core": patch | ||
"@ariakit/react": patch | ||
--- | ||
|
||
The `Checkbox` component now accepts `string[]` as the `value` prop. This is to conform with the native input prop type. If a string array is passed, it will be stringified, just like in the native input element. ([#2456](https://github.com/ariakit/ariakit/pull/2456)) |
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,6 @@ | ||
--- | ||
"@ariakit/react-core": patch | ||
"@ariakit/react": patch | ||
--- | ||
|
||
Fixed the `clickOnEnter` prop on `Checkbox` not working when rendering the component as a native input element. ([#2456](https://github.com/ariakit/ariakit/pull/2456)) |
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,22 @@ | ||
# Group | ||
|
||
<p data-description> | ||
Group related elements in a generic container that may have a label. This abstract component is based on the <a href="https://w3c.github.io/aria/#group">WAI-ARIA Group Role</a>. | ||
</p> | ||
|
||
## API | ||
|
||
<pre data-api> | ||
<<a href="/apis/group">Group</a>> | ||
<<a href="/apis/group-label">GroupLabel</a> /> | ||
</Group> | ||
</pre> | ||
|
||
## Related components | ||
|
||
<div data-cards="components"> | ||
|
||
- [](/components/form) | ||
- [](/components/composite) | ||
|
||
</div> |
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,20 @@ | ||
export default function Icon() { | ||
return ( | ||
<svg viewBox="0 0 128 128" width={128} height={128}> | ||
<foreignObject width={128} height={128}> | ||
<div className="flex h-full w-full items-center justify-center p-4"> | ||
<div className="w-20 gap-2 rounded bg-blue-600 p-2"> | ||
<svg | ||
fill="none" | ||
viewBox="0 0 24 24" | ||
strokeWidth={4} | ||
className="h-6 w-6 stroke-white" | ||
> | ||
<path d="M4.5 12.75l6 6 9-13.5" /> | ||
</svg> | ||
</div> | ||
</div> | ||
</foreignObject> | ||
</svg> | ||
); | ||
} |
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 was deleted.
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,48 @@ | ||
import { forwardRef, useState } from "react"; | ||
import type { ComponentPropsWithoutRef, ReactNode } from "react"; | ||
import * as Ariakit from "@ariakit/react"; | ||
|
||
interface CheckboxProps extends ComponentPropsWithoutRef<"input"> { | ||
children?: ReactNode; | ||
} | ||
|
||
export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>( | ||
function Checkbox({ children, ...props }, ref) { | ||
const [checked, setChecked] = useState(props.defaultChecked ?? false); | ||
const [focusVisible, setFocusVisible] = useState(false); | ||
return ( | ||
<label | ||
className="checkbox" | ||
data-checked={checked} | ||
data-focus-visible={focusVisible || undefined} | ||
> | ||
<Ariakit.VisuallyHidden> | ||
<Ariakit.Checkbox | ||
{...props} | ||
ref={ref} | ||
clickOnEnter | ||
onFocusVisible={() => setFocusVisible(true)} | ||
onBlur={() => setFocusVisible(false)} | ||
onChange={(event) => { | ||
setChecked(event.target.checked); | ||
props.onChange?.(event); | ||
}} | ||
/> | ||
</Ariakit.VisuallyHidden> | ||
<div className="check" data-checked={checked}> | ||
<svg | ||
fill="none" | ||
stroke="currentColor" | ||
strokeWidth="2" | ||
viewBox="0 0 16 16" | ||
height="1em" | ||
width="1em" | ||
> | ||
<polyline points="4,8 7,12 12,4" /> | ||
</svg> | ||
</div> | ||
{children} | ||
</label> | ||
); | ||
} | ||
); |
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 |
---|---|---|
@@ -1,24 +1,6 @@ | ||
import { useState } from "react"; | ||
import * as Ariakit from "@ariakit/react"; | ||
import { Checkbox } from "./checkbox.jsx"; | ||
import "./style.css"; | ||
|
||
export default function Example() { | ||
const checkbox = Ariakit.useCheckboxStore({ defaultValue: false }); | ||
const [focusVisible, setFocusVisible] = useState(false); | ||
const checked = checkbox.useState("value"); | ||
return ( | ||
<label className="label"> | ||
<Ariakit.VisuallyHidden> | ||
<Ariakit.Checkbox | ||
store={checkbox} | ||
onFocusVisible={() => setFocusVisible(true)} | ||
onBlur={() => setFocusVisible(false)} | ||
/> | ||
</Ariakit.VisuallyHidden> | ||
<div className="checkbox" data-focus-visible={focusVisible ? "" : null}> | ||
<Ariakit.CheckboxCheck checked={checked} /> | ||
</div> | ||
I have read and agree to the terms and conditions | ||
</label> | ||
); | ||
return <Checkbox defaultChecked>Ariakit</Checkbox>; | ||
} |
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,23 @@ | ||
export default function Icon() { | ||
return ( | ||
<svg viewBox="0 0 128 128" width={128} height={128}> | ||
<foreignObject width={128} height={128}> | ||
<div className="flex h-full items-center justify-center p-4"> | ||
<div className="flex flex-col gap-2 rounded-lg border-[3px] border-blue-600 bg-white p-2 dark:bg-gray-700"> | ||
<div className="flex h-10 w-10 items-center justify-center rounded-full bg-blue-600 p-2"> | ||
<svg | ||
fill="none" | ||
viewBox="0 0 24 24" | ||
strokeWidth={4} | ||
className="h-full w-full stroke-white" | ||
> | ||
<path d="M4.5 12.75l6 6 9-13.5" /> | ||
</svg> | ||
</div> | ||
<div className="h-2 w-14 bg-black/50 dark:bg-white/50" /> | ||
</div> | ||
</div> | ||
</foreignObject> | ||
</svg> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,19 +1,42 @@ | ||
@import url("../checkbox/style.css"); | ||
|
||
.checkbox { | ||
@apply | ||
w-5 | ||
h-5 | ||
rounded | ||
flex | ||
justify-center | ||
items-center | ||
border | ||
text-blue-900 | ||
bg-blue-200/40 | ||
border-blue-600 | ||
dark:text-blue-100 | ||
dark:bg-blue-600/25 | ||
dark:border-blue-200/40 | ||
gap-2 | ||
p-4 | ||
pr-6 | ||
rounded-lg | ||
select-none | ||
border-2 | ||
border-black/30 | ||
dark:border-white/30 | ||
data-[checked=true]:border-blue-600 | ||
data-[checked=true]:dark:border-blue-600 | ||
bg-white | ||
dark:bg-gray-700 | ||
shadow | ||
dark:shadow-dark | ||
data-[focus-visible]:outline | ||
data-[focus-visible]:outline-4 | ||
data-[focus-visible]:outline-blue-600/25 | ||
; | ||
} | ||
|
||
.check { | ||
@apply | ||
block | ||
rounded-full | ||
p-0.5 | ||
text-lg | ||
bg-gray-150 | ||
dark:bg-gray-850 | ||
[border:inherit] | ||
[stroke-dasharray:15] | ||
[stroke-dashoffset:15] | ||
data-[checked=true]:bg-blue-600 | ||
data-[checked=true]:dark:bg-blue-600 | ||
data-[checked=true]:text-white | ||
data-[checked=true]:[stroke-dashoffset:0] | ||
transition-[stroke-dashoffset] | ||
; | ||
} |
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 |
---|---|---|
@@ -1,18 +1,27 @@ | ||
import { click, getByRole, press } from "@ariakit/test"; | ||
|
||
test("check/uncheck on click", async () => { | ||
expect(getByRole("checkbox")).not.toBeChecked(); | ||
await click(getByRole("checkbox")); | ||
expect(getByRole("checkbox")).toBeChecked(); | ||
await click(getByRole("checkbox")); | ||
expect(getByRole("checkbox")).not.toBeChecked(); | ||
await click(getByRole("checkbox")); | ||
expect(getByRole("checkbox")).toBeChecked(); | ||
}); | ||
|
||
test("check/uncheck on space", async () => { | ||
expect(getByRole("checkbox")).not.toBeChecked(); | ||
expect(getByRole("checkbox")).toBeChecked(); | ||
await press.Tab(); | ||
await press.Space(); | ||
expect(getByRole("checkbox")).toBeChecked(); | ||
expect(getByRole("checkbox")).not.toBeChecked(); | ||
await press.Space(); | ||
expect(getByRole("checkbox")).toBeChecked(); | ||
}); | ||
|
||
test("check/uncheck on enter", async () => { | ||
expect(getByRole("checkbox")).toBeChecked(); | ||
await press.Tab(); | ||
await press.Enter(); | ||
expect(getByRole("checkbox")).not.toBeChecked(); | ||
await press.Enter(); | ||
expect(getByRole("checkbox")).toBeChecked(); | ||
}); |
Oops, something went wrong.
0f58e63
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ariakit – ./
ariakit.org
ariakit-ariakit.vercel.app
ariakit-git-main-ariakit.vercel.app
www.ariakit.org