|
| 1 | +import React from 'react'; |
| 2 | +import { useTheme } from 'native-base'; |
| 3 | +import { Platform } from 'react-native'; |
| 4 | +import type { IHiddenProps } from './types'; |
| 5 | +import { useColorMode } from '../../../core/color-mode/hooks'; |
| 6 | + |
| 7 | +export const HiddenSSR = React.memo(({ children, ...props }: IHiddenProps) => { |
| 8 | + const theme = useTheme(); |
| 9 | + const breakPoints = Object.keys(theme.breakpoints); |
| 10 | + const currentColorMode = useColorMode(); |
| 11 | + |
| 12 | + const { from, till, only, colorMode, platform } = props; |
| 13 | + |
| 14 | + if (children === null) return null; |
| 15 | + if (!from && !till && !only && !colorMode && !platform) { |
| 16 | + return null; |
| 17 | + } else if ( |
| 18 | + (Array.isArray(platform) && platform.includes(Platform.OS)) || |
| 19 | + platform === Platform.OS |
| 20 | + ) { |
| 21 | + return null; |
| 22 | + } else if (colorMode === currentColorMode.colorMode) { |
| 23 | + return null; |
| 24 | + } |
| 25 | + const display: any = {}; |
| 26 | + |
| 27 | + if (till) { |
| 28 | + let flag = false; |
| 29 | + for (const i in breakPoints) { |
| 30 | + if (breakPoints[i] === till) { |
| 31 | + display[breakPoints[i]] = 'flex'; |
| 32 | + flag = true; |
| 33 | + } else { |
| 34 | + display[breakPoints[i]] = flag ? 'flex' : 'none'; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (from) { |
| 40 | + let flag = false; |
| 41 | + for (const i in breakPoints) { |
| 42 | + if (breakPoints[i] === from || flag) { |
| 43 | + display[breakPoints[i]] = 'none'; |
| 44 | + flag = true; |
| 45 | + } else { |
| 46 | + display[breakPoints[i]] = 'flex'; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + if (only) { |
| 51 | + if (Array.isArray(only)) { |
| 52 | + for (const i in breakPoints) { |
| 53 | + if (only.includes(breakPoints[i])) { |
| 54 | + display[breakPoints[i]] = 'none'; |
| 55 | + } else { |
| 56 | + display[breakPoints[i]] = 'flex'; |
| 57 | + } |
| 58 | + } |
| 59 | + } else { |
| 60 | + display[only] = 'none'; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return React.cloneElement(children, { |
| 65 | + display: display, |
| 66 | + }); |
| 67 | +}); |
0 commit comments