-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new ToggleGroup component (#378)
- Loading branch information
Showing
11 changed files
with
1,409 additions
and
1 deletion.
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,42 @@ | ||
.toggle-group { | ||
display: flex; | ||
padding: 0.5rem; | ||
gap: 0.5rem; | ||
} | ||
|
||
.toggle-group[data-orientation="vertical"] { | ||
flex-direction: column; | ||
} | ||
|
||
.toggle-group__item { | ||
padding: 0.5rem; | ||
border-radius: 0.5rem; | ||
appearance: none; | ||
user-select: none; | ||
outline: none; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
transition-property: background-color, color; | ||
transition-duration: 150ms; | ||
transition-timing-function: ease-in-out; | ||
} | ||
|
||
.toggle-group__item:hover { | ||
background-color: hsl(200 98% 39% / 0.8); | ||
color: hsla(0 100% 100% / 0.9); | ||
} | ||
|
||
[data-kb-theme="dark"] .toggle-group__item:hover { | ||
background-color: hsl(200 98% 39% / 0.5); | ||
} | ||
|
||
.toggle-group__item[data-pressed] { | ||
background-color: hsl(200 98% 39%); | ||
color: hsla(0 100% 100% / 0.9); | ||
} | ||
|
||
.toggle-group__item:focus-visible { | ||
outline: 2px solid hsl(200 98% 39%); | ||
outline-offset: 2px; | ||
} |
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,321 @@ | ||
import { ToggleGroup } from "@kobalte/core"; | ||
|
||
import { JSXElement, createSignal } from "solid-js"; | ||
import style from "./toggle-group.module.css"; | ||
|
||
export function BasicExample() { | ||
return ( | ||
<ToggleGroup.Root class={style["toggle-group"]}> | ||
<ToggleGroup.Item | ||
class={style["toggle-group__item"]} | ||
value="bold" | ||
aria-label="Bold" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24px" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
d="M7 5h6a3.5 3.5 0 0 1 0 7H7zm6 7h1a3.5 3.5 0 0 1 0 7H7v-7" | ||
/> | ||
<title>Bold</title> | ||
</svg> | ||
</ToggleGroup.Item> | ||
<ToggleGroup.Item | ||
class={style["toggle-group__item"]} | ||
value="italic" | ||
aria-label="Italic" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24px" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
d="M11 5h6M7 19h6m1-14l-4 14" | ||
/> | ||
<title>Italic</title> | ||
</svg> | ||
</ToggleGroup.Item> | ||
<ToggleGroup.Item | ||
class={style["toggle-group__item"]} | ||
value="underline" | ||
aria-label="Underline" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24px" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
d="M7 5v5a5 5 0 0 0 10 0V5M5 19h14" | ||
/> | ||
<title>Underline</title> | ||
</svg> | ||
</ToggleGroup.Item> | ||
</ToggleGroup.Root> | ||
); | ||
} | ||
|
||
export function DefaultValueExample() { | ||
return ( | ||
<ToggleGroup.Root class={style["toggle-group"]} defaultValue="bold"> | ||
<ToggleGroup.Item | ||
class={style["toggle-group__item"]} | ||
value="bold" | ||
aria-label="Bold" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24px" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
d="M7 5h6a3.5 3.5 0 0 1 0 7H7zm6 7h1a3.5 3.5 0 0 1 0 7H7v-7" | ||
/> | ||
<title>Bold</title> | ||
</svg> | ||
</ToggleGroup.Item> | ||
<ToggleGroup.Item | ||
class={style["toggle-group__item"]} | ||
value="italic" | ||
aria-label="Italic" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24px" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
d="M11 5h6M7 19h6m1-14l-4 14" | ||
/> | ||
<title>Italic</title> | ||
</svg> | ||
</ToggleGroup.Item> | ||
<ToggleGroup.Item | ||
class={style["toggle-group__item"]} | ||
value="underline" | ||
aria-label="Underline" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24px" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
d="M7 5v5a5 5 0 0 0 10 0V5M5 19h14" | ||
/> | ||
<title>Underline</title> | ||
</svg> | ||
</ToggleGroup.Item> | ||
</ToggleGroup.Root> | ||
); | ||
} | ||
|
||
export function ControlledExample() { | ||
const [value, setValue] = createSignal("bold"); | ||
|
||
const list: Record<string, JSXElement> = { | ||
bold: <strong>{value()}</strong>, | ||
italic: <i>{value()}</i>, | ||
underline: <u>{value()}</u>, | ||
}; | ||
|
||
const render = () => { | ||
return list[value()]; | ||
}; | ||
|
||
return ( | ||
<> | ||
<ToggleGroup.Root | ||
class={style["toggle-group"]} | ||
value={value()} | ||
onChange={setValue} | ||
> | ||
<ToggleGroup.Item | ||
class={style["toggle-group__item"]} | ||
value="bold" | ||
aria-label="Bold" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24px" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
d="M7 5h6a3.5 3.5 0 0 1 0 7H7zm6 7h1a3.5 3.5 0 0 1 0 7H7v-7" | ||
/> | ||
<title>Bold</title> | ||
</svg> | ||
</ToggleGroup.Item> | ||
<ToggleGroup.Item | ||
class={style["toggle-group__item"]} | ||
value="italic" | ||
aria-label="Italic" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24px" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
d="M11 5h6M7 19h6m1-14l-4 14" | ||
/> | ||
<title>Italic</title> | ||
</svg> | ||
</ToggleGroup.Item> | ||
<ToggleGroup.Item | ||
class={style["toggle-group__item"]} | ||
value="underline" | ||
aria-label="Underline" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24px" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
d="M7 5v5a5 5 0 0 0 10 0V5M5 19h14" | ||
/> | ||
<title>Underline</title> | ||
</svg> | ||
</ToggleGroup.Item> | ||
</ToggleGroup.Root> | ||
<div class="text-sm"> | ||
Your text style is: <span class="capitalize">{render()}.</span> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
export function MultipleSelectionExample() { | ||
return ( | ||
<ToggleGroup.Root | ||
class={style["toggle-group"]} | ||
multiple | ||
defaultValue={["bold", "underline"]} | ||
> | ||
<ToggleGroup.Item | ||
class={style["toggle-group__item"]} | ||
value="bold" | ||
aria-label="Bold" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24px" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
d="M7 5h6a3.5 3.5 0 0 1 0 7H7zm6 7h1a3.5 3.5 0 0 1 0 7H7v-7" | ||
/> | ||
<title>Bold</title> | ||
</svg> | ||
</ToggleGroup.Item> | ||
<ToggleGroup.Item | ||
class={style["toggle-group__item"]} | ||
value="italic" | ||
aria-label="Italic" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24px" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
d="M11 5h6M7 19h6m1-14l-4 14" | ||
/> | ||
<title>Italic</title> | ||
</svg> | ||
</ToggleGroup.Item> | ||
<ToggleGroup.Item | ||
class={style["toggle-group__item"]} | ||
value="underline" | ||
aria-label="Underline" | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24px" | ||
height="24px" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
fill="none" | ||
stroke="currentColor" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
stroke-width="2" | ||
d="M7 5v5a5 5 0 0 0 10 0V5M5 19h14" | ||
/> | ||
<title>Underline</title> | ||
</svg> | ||
</ToggleGroup.Item> | ||
</ToggleGroup.Root> | ||
); | ||
} |
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
Oops, something went wrong.