File tree Expand file tree Collapse file tree 3 files changed +127
-0
lines changed
dev/components/examples/use-timeout Expand file tree Collapse file tree 3 files changed +127
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` tsx
2+ import { useTimeout } from ' bagon-hooks' ;
3+
4+ import { createSignal } from ' solid-js' ;
5+
6+ export function UseTimeoutExample() {
7+ const [awesomeState, setAwesomeState] = createSignal (' not awesome' );
8+ const { start, clear } = useTimeout (() => {
9+ setAwesomeState (' awesome' );
10+ }, 1000 );
11+
12+ return (
13+ <div class = " flex h-full w-full flex-col items-center justify-center gap-x-3 gap-y-3 rounded-md border p-3 py-10 text-center" >
14+ <p class = " text-xl font-medium" >You: { awesomeState ()} </p >
15+ <p class = " max-w-xs text-sm" >
16+ You will become <b >Awesome</b > in 1 second after pressing 'Start'. You can also cancel.
17+ </p >
18+ <div class = " flex gap-4" >
19+ <button
20+ class = { ` w-20 rounded-md bg-primary px-3 py-1.5 text-white transition active:scale-95 ` }
21+ onClick = { () => {
22+ start (1000 );
23+ }}
24+ >
25+ Start
26+ </button >
27+ <button
28+ class = " w-20 rounded-md bg-gray-400 px-3 py-1.5 text-white transition active:scale-95"
29+ onClick = { () => {
30+ clear ();
31+ setAwesomeState (' not awesome' );
32+ }}
33+ >
34+ Cancel
35+ </button >
36+ </div >
37+ </div >
38+ );
39+ }
40+ ```
Original file line number Diff line number Diff line change 1+ import { useTimeout } from 'src' ;
2+ import { ExampleBase } from '../example-base' ;
3+ import Code from './use-timeout.code.mdx' ;
4+
5+ import { createSignal } from 'solid-js' ;
6+ import { useMDXComponents } from 'solid-jsx' ;
7+
8+ export function UseTimeoutExample ( ) {
9+ const [ awesomeState , setAwesomeState ] = createSignal ( 'not awesome' ) ;
10+ const { start, clear } = useTimeout ( ( ) => {
11+ setAwesomeState ( 'awesome' ) ;
12+ } , 1000 ) ;
13+
14+ // @ts -ignore
15+ const components : any = useMDXComponents ( ) ;
16+
17+ return (
18+ < ExampleBase
19+ title = "useTimeout"
20+ description = "Calls function in given timeout"
21+ code = { < Code components = { components } /> }
22+ >
23+ < div class = "flex h-full w-full flex-col items-center justify-center gap-x-3 gap-y-3 rounded-md border p-3 py-10 text-center" >
24+ < p class = "text-xl font-medium" > You: { awesomeState ( ) } </ p >
25+ < p class = "max-w-xs text-sm" >
26+ You will become < b > Awesome</ b > in 1 second after pressing 'Start'. You can also cancel.
27+ </ p >
28+ < div class = "flex gap-4" >
29+ < button
30+ class = { `w-20 rounded-md bg-primary px-3 py-1.5 text-white transition active:scale-95` }
31+ onClick = { ( ) => {
32+ start ( 1000 ) ;
33+ } }
34+ >
35+ Start
36+ </ button >
37+ < button
38+ class = "w-20 rounded-md bg-gray-400 px-3 py-1.5 text-white transition active:scale-95"
39+ onClick = { ( ) => {
40+ clear ( ) ;
41+ setAwesomeState ( 'not awesome' ) ;
42+ } }
43+ >
44+ Cancel
45+ </ button >
46+ </ div >
47+ </ div >
48+ </ ExampleBase >
49+ ) ;
50+ }
Original file line number Diff line number Diff line change 1+ import { createEffect , onCleanup } from 'solid-js' ;
2+
3+ export function useTimeout (
4+ callback : ( ...callbackParams : any [ ] ) => void ,
5+ delay : number ,
6+ options : { autoInvoke : boolean } = { autoInvoke : false } ,
7+ ) {
8+ let timeoutRef : number | null = null ;
9+
10+ const start = ( ...callbackParams : any [ ] ) => {
11+ if ( ! timeoutRef ) {
12+ timeoutRef = window . setTimeout ( ( ) => {
13+ callback ( callbackParams ) ;
14+ timeoutRef = null ;
15+ } , delay ) ;
16+ }
17+ } ;
18+
19+ const clear = ( ) => {
20+ if ( timeoutRef ) {
21+ window . clearTimeout ( timeoutRef ) ;
22+ timeoutRef = null ;
23+ }
24+ } ;
25+
26+ createEffect ( ( ) => {
27+ if ( options . autoInvoke ) {
28+ start ( ) ;
29+ }
30+
31+ onCleanup ( ( ) => {
32+ clear ( ) ;
33+ } ) ;
34+ } ) ;
35+
36+ return { start, clear } ;
37+ }
You can’t perform that action at this time.
0 commit comments