Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

Button.tsx

29 lines723 Bytes

1import { Show, createSignal, useTransition } from 'solid-js'
2import Spinner from './Spinner'
3import type { JSX } from 'solid-js'
4​
5export default function Button(props: {
6 children: JSX.Element
7 onClick: () => void
8}) {
9 const [isPending, startTransition] = useTransition()
10 const [isStarted, setIsStarted] = createSignal(false)
11 const isLoading = () => isStarted() && isPending()
12​
13 return (
14 <button
15 onClick={() => {
16 setIsStarted(true)
17 void startTransition(() => {
18 props.onClick()
19 }).then(() => setIsStarted(false))
20 }}
21 disabled={isLoading()}
22 >
23 {props.children}{' '}
24 <Show when={isLoading()}>
25 <Spinner />
26 </Show>
27 </button>
28 )