components#useMediaQuery TypeScript Examples
The following examples show how to use
components#useMediaQuery.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: media-query.test.tsx From geist-ui with MIT License | 6 votes |
describe('UseMediaQuery with SSR', () => {
beforeAll(() => {
;(window as any).matchMedia = undefined
})
it('should work correctly', () => {
const { result } = renderHook(() => useMediaQuery('xs'))
expect(result.current).toEqual(false)
})
it('should work correctly with ssr match', () => {
const ssrMatchMedia = (query: string) => ({
matches: mediaQuery.match(query, {
width: '100px',
}),
})
const { result } = renderHook(() =>
useMediaQuery('xs', {
ssrMatchMedia,
}),
)
expect(result.current).toEqual(true)
const { result: result2 } = renderHook(() =>
useMediaQuery('sm', {
ssrMatchMedia,
}),
)
expect(result2.current).toEqual(false)
})
})
Example #2
Source File: media-query.test.tsx From geist-ui with MIT License | 6 votes |
describe('UseMediaQuery', () => {
beforeAll(() => {
;(window as any).matchMedia = mediaListMock(1500)
})
it('should work correctly', () => {
const { result } = renderHook(() => useMediaQuery('xs'))
expect(result.current).toEqual(false)
})
it('should hit', () => {
const { result } = renderHook(() => useMediaQuery('lg'))
expect(result.current).toEqual(true)
})
it('different match types should be supported', () => {
const { result } = renderHook(() =>
useMediaQuery('md', {
match: 'up',
}),
)
expect(result.current).toEqual(true)
const { result: result2 } = renderHook(() =>
useMediaQuery('md', {
match: 'down',
}),
)
expect(result2.current).toEqual(false)
})
})
Example #3
Source File: menu.tsx From geist-ui with MIT License | 4 votes |
Menu: React.FC<unknown> = () => {
const router = useRouter()
const theme = useTheme()
const { isChinese } = useConfigs()
const { tabbar: currentUrlTabValue, locale } = useLocale()
const [expanded, setExpanded] = useState<boolean>(false)
const [, setBodyHidden] = useBodyScroll(null, { delayReset: 300 })
const isMobile = useMediaQuery('xs', { match: 'down' })
const allSides = useMemo(() => Metadata[locale], [locale])
useEffect(() => {
const prefetch = async () => {
const urls = isChinese
? ['/zh-cn/guide/introduction', '/zh-cn/components/text', '/zh-cn/customization']
: ['/en-us/guide/introduction', '/en-us/components/text', '/en-us/customization']
await Promise.all(
urls.map(async url => {
await router.prefetch(url)
}),
)
}
prefetch()
.then()
.catch(err => console.log(err))
}, [isChinese])
useEffect(() => {
setBodyHidden(expanded)
}, [expanded])
useEffect(() => {
if (!isMobile) {
setExpanded(false)
}
}, [isMobile])
useEffect(() => {
const handleRouteChange = () => {
setExpanded(false)
}
router.events.on('routeChangeComplete', handleRouteChange)
return () => router.events.off('routeChangeComplete', handleRouteChange)
}, [router.events])
const handleTabChange = useCallback(
(tab: string) => {
const shouldRedirectDefaultPage = currentUrlTabValue !== tab
if (!shouldRedirectDefaultPage) return
const defaultPath = `/${locale}/${tab}`
router.push(defaultPath)
},
[currentUrlTabValue, locale],
)
const [isLocked, setIsLocked] = useState<boolean>(false)
useEffect(() => {
const handler = () => {
const isLocked = document.body.style.overflow === 'hidden'
setIsLocked(last => (last !== isLocked ? isLocked : last))
}
const observer = new MutationObserver(mutations => {
mutations.forEach(function (mutation) {
if (mutation.type !== 'attributes') return
handler()
})
})
observer.observe(document.body, {
attributes: true,
})
return () => {
observer.disconnect()
}
}, [])
return (
<>
<div className="menu-wrapper">
<nav className="menu">
<div className="content">
<div className="logo">
<NextLink href={`/${locale}`}>
<a aria-label="Go Home">
<Image
src="/images/logo.png"
width="20px"
height="20px"
mr={0.5}
draggable={false}
title="Logo"
/>
Geist
</a>
</NextLink>
</div>
<div className="tabs">
<Tabs
value={currentUrlTabValue}
leftSpace={0}
activeClassName="current"
align="center"
hideDivider
hideBorder
onChange={handleTabChange}>
<Tabs.Item font="14px" label={isChinese ? '主页' : 'Home'} value="" />
{allSides.map((tab, index) => (
<Tabs.Item
font="14px"
label={tab.localeName || tab.name}
value={tab.name}
key={`${tab.localeName || tab.name}-${index}`}
/>
))}
</Tabs>
</div>
<div className="controls">
{isMobile ? (
<Button
className="menu-toggle"
auto
type="abort"
onClick={() => setExpanded(!expanded)}>
<MenuIcon size="1.125rem" />
</Button>
) : (
<Controls />
)}
</div>
</div>
</nav>
</div>
<MenuMobile expanded={expanded} />
<style jsx>{`
.menu-wrapper {
height: var(--geist-page-nav-height);
}
.menu {
position: fixed;
top: 0;
left: 0;
right: 0;
padding-right: ${isLocked ? 'var(--geist-page-scrollbar-width)' : 0};
height: var(--geist-page-nav-height);
//width: 100%;
backdrop-filter: saturate(180%) blur(5px);
background-color: ${addColorAlpha(theme.palette.background, 0.8)};
box-shadow: ${theme.type === 'dark'
? '0 0 0 1px #333'
: '0 0 15px 0 rgba(0, 0, 0, 0.1)'};
z-index: 999;
}
nav .content {
display: flex;
align-items: center;
justify-content: space-between;
max-width: 1000px;
height: 100%;
margin: 0 auto;
user-select: none;
padding: 0 ${theme.layout.gap};
}
.logo {
flex: 1 1;
display: flex;
align-items: center;
justify-content: flex-start;
}
.logo a {
display: inline-flex;
flex-direction: row;
align-items: center;
font-size: 1.125rem;
font-weight: 500;
color: inherit;
height: 28px;
}
.logo :global(.image) {
border: 1px solid ${theme.palette.border};
border-radius: 2rem;
}
.tabs {
flex: 1 1;
padding: 0 ${theme.layout.gap};
}
.tabs :global(.content) {
display: none;
}
@media only screen and (max-width: ${theme.breakpoints.xs.max}) {
.tabs {
display: none;
}
}
.controls {
flex: 1 1;
display: flex;
align-items: center;
justify-content: flex-end;
}
.controls :global(.menu-toggle) {
display: flex;
align-items: center;
min-width: 40px;
height: 40px;
padding: 0;
}
`}</style>
</>
)
}