Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ export function Toggle({ children }: { children: React.ReactNode }) {
return <ToggleContext value={{ on, toggle }}>{children}</ToggleContext>
}

// 🐨 create a custom useToggle() hook here
// create a new context variable and read ToggleContext with use
// when no context is found, throw an error with a useful message
// otherwise return the context

export function ToggleOn({ children }: { children: React.ReactNode }) {
// 🐨 instead reading context with use, we'll need to get that from useToggle()
const { on } = use(ToggleContext)!
return <>{on ? children : null}</>
}

export function ToggleOff({ children }: { children: React.ReactNode }) {
// 🐨 instead reading context with use, we'll need to get that from useToggle()
const { on } = use(ToggleContext)!
return <>{on ? null : children}</>
}
Expand All @@ -25,6 +32,7 @@ type ToggleButtonProps = Omit<React.ComponentProps<typeof Switch>, 'on'> & {
on?: boolean
}
export function ToggleButton({ ...props }: ToggleButtonProps) {
// 🐨 instead reading context with use, we'll need to get that from useToggle()
const { on, toggle } = use(ToggleContext)!
return <Switch {...props} on={on} onClick={toggle} />
}