generated from algolia/pwa-ecom-ui-template
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmedia.tsx
54 lines (44 loc) · 1.36 KB
/
media.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { createMedia } from '@artsy/fresnel'
import classNames from 'classnames'
import type { PropsWithChildren } from 'react'
import tailwindScreens from '@/utils/tailwindScreens'
type BreakpointProps = PropsWithChildren<{
className?: string
}>
export type Breakpoints = 'laptop' | 'mobile' | 'tablet'
export const AppMedia = createMedia({
breakpoints: {
mobile: 0,
...tailwindScreens,
},
})
export const mediaStyles = AppMedia.createMediaStyle()
export const { Media, MediaContextProvider } = AppMedia
const getMediaRender = (children: React.ReactNode, className?: string) => {
return function MediaRender(
mediaClassNames: string,
renderChildren: React.ReactNode
) {
return (
<div
className={classNames(mediaClassNames, className)}
suppressHydrationWarning={!renderChildren}
>
{renderChildren ? children : null}
</div>
)
}
}
export function Mobile({ children, className }: BreakpointProps) {
return <Media at="mobile">{getMediaRender(children, className)}</Media>
}
export function Tablet({ children, className }: BreakpointProps) {
return <Media lessThan="laptop">{getMediaRender(children, className)}</Media>
}
export function Laptop({ children, className }: BreakpointProps) {
return (
<Media greaterThanOrEqual="laptop">
{getMediaRender(children, className)}
</Media>
)
}