@@ -14,31 +14,53 @@ import {
1414 useStyles$ ,
1515 useStylesScoped$ ,
1616 createContextId ,
17- Resource
17+ Resource ,
18+ useAsyncComputed$ ,
19+ useSerializer$ ,
20+ useConstant ,
21+ useOn ,
22+ useServerData ,
23+ useErrorBoundary ,
1824} from '@qwik.dev/core' ;
1925import { _getDomContainer , isServer , useVisibleTask$ } from '@qwik.dev/core/internal' ;
20- import type { QRL } from '@qwik.dev/core' ;
21-
26+ import type { QRL , Signal } from '@qwik.dev/core' ;
27+ import { useLocation , useNavigate , Link , useContent , useDocumentHead } from '@qwik.dev/router' ;
28+ import { useDebouncer } from './debounce' ;
2229const ButtonContext = createContextId < { theme : string ; size : string } > ( 'button-context' ) ;
2330
2431interface ButtonProps {
2532 class ?: string ;
2633 onClick$ ?: QRL < ( ) => void > ;
34+ testValue : Signal < string > ;
2735}
2836
29- export default component$ < ButtonProps > ( ( { class : className = '' , onClick$ } ) => {
30-
37+ export default component$ < ButtonProps > ( ( props ) => {
38+ const { class : className = '' , onClick$ } = props ;
39+ const testValue2 = props . testValue
40+ console . log ( 'testValue' , testValue2 )
3141 const store = useStore ( {
3242 count : 0 ,
43+ dd :12 ,
44+ cc : 33 ,
45+ aa : [ 1 , 2 , 3 ]
3346 } ) ;
34- const signal = useSignal ( '111' ) ;
47+ const signal = useSignal < any > ( '111' ) ;
48+ const constantValue = useConstant ( ( ) => 'CONST' ) ;
49+ const serverData = useServerData < any > ( 'demo-key' ) ;
50+ const errorBoundary = useErrorBoundary ( ) ;
51+ const location = useLocation ( ) ;
52+ const navigate = useNavigate ( ) ;
53+ const content = useContent ( ) ;
54+ const head = useDocumentHead ( ) ;
55+
3556
3657
3758 useTask$ ( ( { track } ) => {
3859 track ( ( ) => store . count ) ;
3960 signal . value = '33333'
4061 } )
4162
63+ // eslint-disable-next-line qwik/no-use-visible-task
4264 useVisibleTask$ ( ( { track } ) => {
4365 track ( ( ) => store . count ) ;
4466 signal . value = '2227'
@@ -57,14 +79,15 @@ export default component$<ButtonProps>(({ class: className = '', onClick$ }) =>
5779 }
5880 } ) ;
5981
60- const asyncComputedValue = useComputed $( ( ) => {
61- return `Async computed: ${ store . count } ` ;
62- } ) ;
63-
82+ const asyncComputedValue = useAsyncComputed $( ( { track } ) =>
83+ Promise . resolve ( track ( signal ) + 3 ) ,
84+ ) ;
85+
6486 useContextProvider ( ButtonContext , {
6587 theme : 'primary' ,
6688 size : 'large'
6789 } ) ;
90+
6891 const context = useContext ( ButtonContext ) ;
6992
7093 const buttonId = useId ( ) ;
@@ -77,6 +100,10 @@ export default component$<ButtonProps>(({ class: className = '', onClick$ }) =>
77100 console . log ( 'Window resized' ) ;
78101 } ) ) ;
79102
103+ useOn ( 'click' , $ ( ( ) => {
104+ console . log ( 'Host clicked' ) ;
105+ } ) ) ;
106+
80107 const resourceData = useResource$ ( async ( { track } ) => {
81108 track ( ( ) => store . count ) ;
82109 await new Promise ( resolve => setTimeout ( resolve , 200 ) ) ;
@@ -86,6 +113,14 @@ export default component$<ButtonProps>(({ class: className = '', onClick$ }) =>
86113 } ;
87114 } ) ;
88115
116+ const customSerialized = useSerializer$ ( ( ) => ( {
117+ deserialize : ( ) => ( { n : store . count } ) ,
118+ update : ( current : { n : number } ) => {
119+ current . n = store . count ;
120+ return current ;
121+ }
122+ } ) ) ;
123+
89124 useStyles$ ( `
90125 .custom-button {
91126 background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
@@ -111,30 +146,65 @@ export default component$<ButtonProps>(({ class: className = '', onClick$ }) =>
111146 cursor: pointer;
112147 }
113148 ` ) ;
149+ const debounce = useDebouncer (
150+ $ ( ( value : string ) => {
151+ signal . value = value ;
152+ } ) ,
153+ 1000
154+ ) ;
155+
156+ useDebouncer ( $ ( ( value : string ) => {
157+ signal . value = value ;
158+ } ) ,
159+ 1000 )
114160
115161 const handleClick = $ ( async ( ) => {
116162 store . count ++ ;
117163 console . log ( 'Button clicked! Count:' , store . count ) ;
118-
164+ debounce ( store . count )
119165 if ( onClick$ ) {
120166 await onClick$ ( ) ;
121167 }
122168 } ) ;
123169
170+ const handleGoAbout = $ ( ( ) => navigate ( '/about' ) ) ;
171+
124172 return (
125173 < div >
126174 < button
127175 id = { buttonId }
128176 class = { `${ className } custom-button scoped-button` }
129177 onClick$ = { handleClick }
130178 >
131- Click me { store . count } { signal . value } { qwikContainer ?. value ?. qManifestHash }
179+ Click me { store . count } { signal . value } { qwikContainer . value ?. qManifestHash }
180+ </ button >
181+ < button
182+ class = { `custom-button scoped-button` }
183+ style = "margin-left: 8px"
184+ onClick$ = { handleGoAbout }
185+ >
186+ Go /about
132187 </ button >
188+ < Link href = "/blog" class = { `scoped-button` } style = "margin-left: 8px; padding: 8px 16px; display: inline-block; text-decoration: none;" >
189+ Go /blog
190+ </ Link >
133191
134192 < div style = "margin-top: 10px; font-size: 12px; color: #666;" >
193+ < div > Current Path: { location . url . pathname } </ div >
194+ < div > Is Navigating: { location . isNavigating ? 'true' : 'false' } </ div >
195+ < div > Params: { JSON . stringify ( location . params ) } </ div >
196+ < div > Prev URL: { location . prevUrl ? location . prevUrl . pathname : '—' } </ div >
197+ < div > Head Title: { head . title } </ div >
198+ < div > Head Metas: { head . meta . length } </ div >
199+ < div > Content Menu: { content . menu ? 'yes' : 'no' } </ div >
200+ < div > Content Headings: { content . headings ? content . headings . length : 0 } </ div >
135201 < div > Async Computed: { asyncComputedValue . value } </ div >
136- < div > Context: { context ? .theme } - { context ? .size } </ div >
202+ < div > Context: { context . theme } - { context . size } </ div >
137203 < div > Button ID: { buttonId } </ div >
204+ < div > Constant: { constantValue } </ div >
205+ { errorBoundary . error && < div > Error captured</ div > }
206+ < div > ServerData: { serverData ? JSON . stringify ( serverData ) : 'N/A' } </ div >
207+ < div > Serialized N: { customSerialized . value . n } </ div >
138208 < Resource
139209 value = { resourceData }
140210 onPending = { ( ) => < div > Loading resource...</ div > }
0 commit comments