Skip to content

Commit

Permalink
toggle working, needs more flexibility (e.g. dropdown menu with butto…
Browse files Browse the repository at this point in the history
…n element), fix TS error from Switch
  • Loading branch information
mewdev committed Sep 22, 2024
1 parent 456a774 commit 897e9a3
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions packages/design-system/src/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,18 @@ import React from "react";
import { Switch } from "@headlessui/react";
import { useState, Fragment } from "react";

const Toggle = () => {
type Props = {
children: (checked: boolean) => React.ReactNode;
};

const Toggle = ({ children }: Props): JSX.Element => {
const [enabled, setEnabled] = useState(false);

return (
<Switch checked={enabled} onChange={setEnabled} as={Fragment}>
{({ checked }) =>
// Approach 1: (conditional styling of the children element)

// <button className={checked ? "k1-bg-blue-600" : "k1-bg-gray-200"}>
// Toggle button
// </button>

// Approach 2: (crendering different children elements based on the checked state)

checked ? (
// toggle checked
<button className="k1-bg-green-300">Button checked</button>
) : (
// toggle checked
<button className="k1-bg-red-400">Button not checked</button>
)
}
{({ checked }) => {
return children(checked);
}}
</Switch>
);
};
Expand Down

0 comments on commit 897e9a3

Please sign in to comment.