|
| 1 | +import { SpatialNavigationNode, SpatialNavigationView } from 'react-tv-space-navigation'; |
| 2 | +import { Page } from '../components/Page'; |
| 3 | +import { Button } from '../design-system/components/Button'; |
| 4 | +import { Typography } from '../design-system/components/Typography'; |
| 5 | +import { Box } from '../design-system/components/Box'; |
| 6 | +import { Spacer } from '../design-system/components/Spacer'; |
| 7 | +import { useState } from 'react'; |
| 8 | +import { ActivityIndicator } from 'react-native'; |
| 9 | +import styled from '@emotion/native'; |
| 10 | + |
| 11 | +function sleep(ms) { |
| 12 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 13 | +} |
| 14 | + |
| 15 | +export const AsynchronousContent = () => { |
| 16 | + const [shouldShow, setShouldShow] = useState(false); |
| 17 | + const [isLoading, setIsLoading] = useState(false); |
| 18 | + |
| 19 | + const toggle = async () => { |
| 20 | + if (isLoading) return; |
| 21 | + |
| 22 | + setIsLoading(true); |
| 23 | + await sleep(1000); |
| 24 | + setIsLoading(false); |
| 25 | + |
| 26 | + setShouldShow((prev) => !prev); |
| 27 | + }; |
| 28 | + |
| 29 | + return ( |
| 30 | + <Page> |
| 31 | + <Box padding={'$8'}> |
| 32 | + <Typography> |
| 33 | + { |
| 34 | + 'Here are some details about a common pitfall with this library: how do I show/hide focusable elements properly?' |
| 35 | + } |
| 36 | + </Typography> |
| 37 | + <Typography>{'Use this button to trigger an asynchronous show/hide'}</Typography> |
| 38 | + <SpatialNavigationView direction="horizontal"> |
| 39 | + <Button label={shouldShow ? 'Hide' : 'Show'} onSelect={toggle} /> |
| 40 | + <Spacer gap="$8" direction="horizontal" /> |
| 41 | + {isLoading && <ActivityIndicator color="white" size={'large'} />} |
| 42 | + </SpatialNavigationView> |
| 43 | + <Spacer gap="$8" /> |
| 44 | + <Typography> |
| 45 | + { |
| 46 | + 'Then, try out the focus below. Focus should not be messed up after asynchronous modifications :)' |
| 47 | + } |
| 48 | + </Typography> |
| 49 | + <StyledNavigationRow direction="horizontal"> |
| 50 | + <Button label="First button" /> |
| 51 | + <SpatialNavigationNode> |
| 52 | + {shouldShow && <Button label="Second button (asynchronously showed)" />} |
| 53 | + </SpatialNavigationNode> |
| 54 | + <Button label="Third button" /> |
| 55 | + </StyledNavigationRow> |
| 56 | + |
| 57 | + <Typography>Check out the code to understand how it is done.</Typography> |
| 58 | + </Box> |
| 59 | + </Page> |
| 60 | + ); |
| 61 | +}; |
| 62 | + |
| 63 | +const StyledNavigationRow = styled(SpatialNavigationView)({ |
| 64 | + gap: 40, |
| 65 | +}); |
0 commit comments