@@ -4,7 +4,7 @@ import $ from '../index'; // Adjust path as necessary
44import { useFormStatus as reactDom_useFormStatus } from 'react-dom' ; // For useFormStatus example
55
66// --- useState ---
7- function UseStateExample ( ) {
7+ export function Example_useState ( ) {
88 return (
99 < $ >
1010 { ( { useState } ) => {
@@ -19,9 +19,10 @@ function UseStateExample() {
1919 </ $ >
2020 ) ;
2121}
22+ Example_useState . storyName = 'useState' ;
2223
2324// --- useReducer ---
24- function UseReducerExample ( ) {
25+ export function Example_useReducer ( ) {
2526 return (
2627 < $ >
2728 { ( { useReducer } ) => {
@@ -40,9 +41,10 @@ function UseReducerExample() {
4041 </ $ >
4142 ) ;
4243}
44+ Example_useReducer . storyName = 'useReducer' ;
4345
4446// --- useCallback ---
45- function UseCallbackExample ( ) {
47+ export function Example_useCallback ( ) {
4648 return (
4749 < $ >
4850 { ( { useState, useCallback } ) => {
@@ -59,10 +61,11 @@ function UseCallbackExample() {
5961 </ $ >
6062 ) ;
6163}
64+ Example_useCallback . storyName = 'useCallback' ;
6265
6366// --- useContext ---
6467const ThemeCtx = React . createContext < 'light' | 'dark' > ( 'light' ) ;
65- function UseContextExample ( ) {
68+ export function Example_useContext ( ) {
6669 return (
6770 < ThemeCtx . Provider value = "dark" >
6871 < $ >
@@ -71,9 +74,10 @@ function UseContextExample() {
7174 </ ThemeCtx . Provider >
7275 ) ;
7376}
77+ Example_useContext . storyName = 'useContext' ;
7478
7579// --- useMemo ---
76- function UseMemoExample ( ) {
80+ export function Example_useMemo ( ) {
7781 return (
7882 < $ >
7983 { ( { useState, useMemo } ) => {
@@ -98,9 +102,10 @@ function UseMemoExample() {
98102 </ $ >
99103 ) ;
100104}
105+ Example_useMemo . storyName = 'useMemo' ;
101106
102107// --- useEffect ---
103- function UseEffectExample ( ) {
108+ export function Example_useEffect ( ) {
104109 return (
105110 < $ >
106111 { ( { useState, useEffect } ) => {
@@ -114,9 +119,10 @@ function UseEffectExample() {
114119 </ $ >
115120 ) ;
116121}
122+ Example_useEffect . storyName = 'useEffect' ;
117123
118124// --- useLayoutEffect ---
119- function UseLayoutEffectExample ( ) {
125+ export function Example_useLayoutEffect ( ) {
120126 return (
121127 < $ >
122128 { ( { useRef, useLayoutEffect } ) => {
@@ -129,6 +135,7 @@ function UseLayoutEffectExample() {
129135 </ $ >
130136 ) ;
131137}
138+ Example_useLayoutEffect . storyName = 'useLayoutEffect' ;
132139
133140// --- useImperativeHandle ---
134141const FancyInput = React . forwardRef < HTMLInputElement > ( ( _ , ref ) => (
@@ -141,7 +148,7 @@ const FancyInput = React.forwardRef<HTMLInputElement>((_, ref) => (
141148 </ $ >
142149) ) ;
143150FancyInput . displayName = 'FancyInput' ;
144- function UseImperativeHandleExample ( ) {
151+ export function Example_useImperativeHandle ( ) {
145152 const fancyRef = React . useRef < HTMLInputElement > ( null ) ;
146153 return (
147154 < div >
@@ -150,9 +157,10 @@ function UseImperativeHandleExample() {
150157 </ div >
151158 ) ;
152159}
160+ Example_useImperativeHandle . storyName = 'useImperativeHandle' ;
153161
154162// --- useRef ---
155- function UseRefExample ( ) {
163+ export function Example_useRef ( ) {
156164 return (
157165 < $ >
158166 { ( { useRef } ) => {
@@ -167,9 +175,10 @@ function UseRefExample() {
167175 </ $ >
168176 ) ;
169177}
178+ Example_useRef . storyName = 'useRef' ;
170179
171180// --- useInsertionEffect ---
172- function UseInsertionEffectExample ( ) {
181+ export function Example_useInsertionEffect ( ) {
173182 const [ show , setShow ] = React . useState ( true ) ;
174183 const id = 'insertion-effect-style' ;
175184 return (
@@ -194,9 +203,10 @@ function UseInsertionEffectExample() {
194203 </ div >
195204 ) ;
196205}
206+ Example_useInsertionEffect . storyName = 'useInsertionEffect' ;
197207
198208// --- useId ---
199- function UseIdExample ( ) {
209+ export function Example_useId ( ) {
200210 return (
201211 < $ >
202212 { ( { useId, useState } ) => {
@@ -212,9 +222,10 @@ function UseIdExample() {
212222 </ $ >
213223 ) ;
214224}
225+ Example_useId . storyName = 'useId' ;
215226
216227// --- useSyncExternalStore ---
217- function UseSyncExternalStoreExample ( ) {
228+ export function Example_useSyncExternalStore ( ) {
218229 return (
219230 < $ >
220231 { ( { useSyncExternalStore } ) => {
@@ -231,9 +242,10 @@ function UseSyncExternalStoreExample() {
231242 </ $ >
232243 ) ;
233244}
245+ Example_useSyncExternalStore . storyName = 'useSyncExternalStore' ;
234246
235247// --- useDeferredValue ---
236- function UseDeferredValueExample ( ) {
248+ export function Example_useDeferredValue ( ) {
237249 return (
238250 < $ >
239251 { ( { useState, useDeferredValue } ) => {
@@ -249,32 +261,68 @@ function UseDeferredValueExample() {
249261 </ $ >
250262 ) ;
251263}
264+ Example_useDeferredValue . storyName = 'useDeferredValue' ;
252265
253266// --- useTransition ---
254- function UseTransitionExample ( ) {
267+ export function Example_useTransition ( ) {
255268 return (
256269 < $ >
257- { ( { useState, useTransition } ) => {
258- const [ list , setList ] = useState < string [ ] > ( [ ] ) ;
270+ { ( { useState, useTransition, useMemo } ) => {
271+ // Create a fixed list of sample products once (more practical than generic numbers)
272+ const items = useMemo (
273+ ( ) => [
274+ 'Alligator' ,
275+ 'Bear' ,
276+ 'Cat' ,
277+ 'Dog' ,
278+ 'Elephant' ,
279+ 'Fox' ,
280+ 'Giraffe' ,
281+ 'Horse' ,
282+ 'Iguana' ,
283+ 'Jaguar' ,
284+ 'Kangaroo' ,
285+ 'Lion' ,
286+ 'Monkey' ,
287+ 'Newt' ,
288+ 'Owl' ,
289+ 'Penguin' ,
290+ 'Quail' ,
291+ 'Rabbit' ,
292+ 'Shark' ,
293+ 'Tiger' ,
294+ ] ,
295+ [ ] ,
296+ ) ;
297+ const [ list , setList ] = useState < string [ ] > ( items ) ;
259298 const [ pending , start ] = useTransition ( ) ;
299+
260300 const filter = ( e : React . ChangeEvent < HTMLInputElement > ) => {
261- const q = e . target . value ;
262- start ( ( ) => setList ( Array . from ( { length : 1000 } , ( _ , i ) => `Item ${ i } ` ) . filter ( ( x ) => x . includes ( q ) ) ) ) ; // Reduced length for story
301+ const q = e . target . value . toLowerCase ( ) ;
302+ start ( ( ) => setList ( items . filter ( ( x ) => x . toLowerCase ( ) . includes ( q ) ) ) ) ;
263303 } ;
264304 return (
265305 < div >
266- < input onChange = { filter } placeholder = "useTransition: filter 1k items" />
306+ { /* Show the full list so users know what can be searched */ }
307+ < p style = { { maxWidth : '600px' } } >
308+ All items:
309+ < span style = { { fontStyle : 'italic' } } > { items . join ( ', ' ) } </ span >
310+ </ p >
311+ < input aria-label = "useTransition-filter" onChange = { filter } placeholder = "Filter products..." />
267312 { pending && < p > Updating...</ p > }
268- < p > { list . length } items found.</ p >
313+ < p > { list . length } item{ list . length === 1 ? '' : 's' } found. { list . length === items . length ? '(no items filtered)' : '' } </ p >
314+ { /* Show the filtered items */ }
315+ { list . length > 0 && < p style = { { maxWidth : '600px' } } > { list . join ( ', ' ) } </ p > }
269316 </ div >
270317 ) ;
271318 } }
272319 </ $ >
273320 ) ;
274321}
322+ Example_useTransition . storyName = 'useTransition' ;
275323
276324// --- useActionState ---
277- function UseActionStateExample ( ) {
325+ export function Example_useActionState ( ) {
278326 if ( ! React . useActionState ) {
279327 return < p > React.useActionState is not available in this version of React.</ p > ;
280328 }
@@ -299,7 +347,7 @@ function UseActionStateExample() {
299347 </ $ >
300348 ) ;
301349}
302-
350+ Example_useActionState . storyName = 'useActionState' ;
303351
304352// --- useFormStatus ---
305353// Note: react-dom useFormStatus is used here as react's might not be available/same
@@ -315,7 +363,7 @@ const FormStatusButton = () => {
315363 </ $ >
316364 ) ;
317365} ;
318- function UseFormStatusExample ( ) {
366+ export function Example_useFormStatus ( ) {
319367 if ( ! reactDom_useFormStatus ) { // Check if the imported one exists
320368 return < p > ReactDOM.useFormStatus is not available in this version of React DOM.</ p > ;
321369 }
@@ -338,6 +386,7 @@ function UseFormStatusExample() {
338386 </ $ >
339387 ) ;
340388}
389+ Example_useFormStatus . storyName = 'useFormStatus' ;
341390
342391// --- use (awaitable hook) ---
343392// Helper function for the 'use' example
@@ -349,7 +398,7 @@ const fetchQuote = () => {
349398 return quotePromise ;
350399} ;
351400
352- function UseAwaitExample ( ) {
401+ export function Example_use ( ) {
353402 if ( ! React . use ) {
354403 return < p > React.use is not available in this version of React.</ p > ;
355404 }
@@ -367,8 +416,9 @@ function UseAwaitExample() {
367416 </ React . Suspense >
368417 ) ;
369418}
419+ Example_use . storyName = 'use' ;
370420
371-
421+ // --- meta ---
372422const meta : Meta = {
373423 title : 'Examples/Built-in Hooks' ,
374424 tags : [ 'autodocs' ] ,
@@ -384,21 +434,3 @@ const meta: Meta = {
384434 ] ,
385435} ;
386436export default meta ;
387-
388- export const State : StoryObj = { name : 'useState' , render : ( ) => < UseStateExample /> } ;
389- export const Reducer : StoryObj = { name : 'useReducer' , render : ( ) => < UseReducerExample /> } ;
390- export const Callback : StoryObj = { name : 'useCallback' , render : ( ) => < UseCallbackExample /> } ;
391- export const Context : StoryObj = { name : 'useContext' , render : ( ) => < UseContextExample /> } ;
392- export const Memo : StoryObj = { name : 'useMemo' , render : ( ) => < UseMemoExample /> } ;
393- export const Effect : StoryObj = { name : 'useEffect' , render : ( ) => < UseEffectExample /> } ;
394- export const LayoutEffect : StoryObj = { name : 'useLayoutEffect' , render : ( ) => < UseLayoutEffectExample /> } ;
395- export const ImperativeHandle : StoryObj = { name : 'useImperativeHandle' , render : ( ) => < UseImperativeHandleExample /> } ;
396- export const Ref : StoryObj = { name : 'useRef' , render : ( ) => < UseRefExample /> } ;
397- export const InsertionEffect : StoryObj = { name : 'useInsertionEffect' , render : ( ) => < UseInsertionEffectExample /> } ;
398- export const Id : StoryObj = { name : 'useId' , render : ( ) => < UseIdExample /> } ;
399- export const SyncExternalStore : StoryObj = { name : 'useSyncExternalStore' , render : ( ) => < UseSyncExternalStoreExample /> } ;
400- export const DeferredValue : StoryObj = { name : 'useDeferredValue' , render : ( ) => < UseDeferredValueExample /> } ;
401- export const Transition : StoryObj = { name : 'useTransition' , render : ( ) => < UseTransitionExample /> } ;
402- export const ActionState : StoryObj = { name : 'useActionState' , render : ( ) => < UseActionStateExample /> } ;
403- export const FormStatus : StoryObj = { name : 'useFormStatus' , render : ( ) => < UseFormStatusExample /> } ;
404- export const AwaitUse : StoryObj = { name : 'use (awaitable)' , render : ( ) => < UseAwaitExample /> } ;
0 commit comments