-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor SwitchField to Toggle component
- Loading branch information
Showing
6 changed files
with
48 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,36 @@ | ||
import { Switch } from "@kobalte/core"; | ||
import { FiCheck } from "solid-icons/fi"; | ||
import { VsClose } from "solid-icons/vs"; | ||
import { Accessor, Component, Setter } from "solid-js"; | ||
import { Component, Setter } from "solid-js"; | ||
|
||
type SwitchProps = { | ||
checked?: Accessor<boolean> | boolean; | ||
checked?: boolean; | ||
onChange: (val: boolean) => Promise<void> | Setter<boolean> | undefined; | ||
}; | ||
|
||
const SwitchField: Component<SwitchProps> = (props) => { | ||
const getChecked = () => (typeof props.checked === "function" ? props.checked() : props.checked); | ||
|
||
const Toggle: Component<SwitchProps> = (props) => { | ||
|
||
return ( | ||
<Switch.Root class="mx-1 inline-flex cursor-pointer items-center" checked={getChecked()} onChange={props.onChange}> | ||
<Switch.Input /> | ||
<Switch.Control class="inline-flex h-4 w-11 items-center rounded-xl bg-red-600 bg-opacity-20 transition-colors kb-checked:bg-indigo-600"> | ||
<Switch.Thumb class="inline-flex h-4 w-4 items-center justify-center rounded-lg bg-white p-0.5 transition-all kb-checked:translate-x-[calc(172%)] dark:bg-zinc-700"> | ||
{getChecked() ? <FiCheck /> : <VsClose />} | ||
</Switch.Thumb> | ||
</Switch.Control> | ||
</Switch.Root> | ||
<> | ||
<label class="relative inline-flex cursor-pointer items-center"> | ||
<input | ||
type="checkbox" | ||
checked={props.checked} | ||
onChange={() => props.onChange(!props.checked)} | ||
class="peer sr-only" | ||
/> | ||
<div class="peer h-5 w-11 rounded-full bg-gray-200 after:absolute after:start-[2px] after:h-5 after:w-5 after:rounded-full after:border after:border-gray-300 after:bg-white after:dark:bg-zinc-700 after:transition-all after:content-[''] peer-checked:bg-indigo-600 peer-checked:after:translate-x-full peer-checked:after:border-white peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 rtl:peer-checked:after:-translate-x-full dark:border-gray-600 dark:bg-gray-700 dark:peer-focus:ring-blue-800"></div> | ||
</label> | ||
<Switch.Root class="mx-1 inline-flex cursor-pointer items-center" checked={props.checked} onChange={props.onChange}> | ||
<Switch.Input /> | ||
<Switch.Control class="inline-flex h-4 w-11 items-center rounded-xl bg-red-600 bg-opacity-20 transition-colors kb-checked:bg-indigo-600"> | ||
<Switch.Thumb class="inline-flex h-4 w-4 items-center justify-center rounded-lg bg-white p-0.5 transition-all kb-checked:translate-x-[calc(172%)] dark:bg-zinc-700"> | ||
{props.checked ? <FiCheck /> : <VsClose />} | ||
</Switch.Thumb> | ||
</Switch.Control> | ||
</Switch.Root> | ||
</> | ||
); | ||
}; | ||
|
||
export default SwitchField; | ||
export default Toggle; |
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
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