about summary refs log tree commit diff
path: root/src/components/Grid.tsx
blob: d424634de46ab8b10a68e525650b4a40d8280cdf (plain) (blame)
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
55
56
57
58
59
import {createContext, useContext, useMemo} from 'react'
import {View} from 'react-native'

import {atoms as a, ViewStyleProp} from '#/alf'

const Context = createContext({
  gap: 0,
})

export function Row({
  children,
  gap = 0,
  style,
}: ViewStyleProp & {
  children: React.ReactNode
  gap?: number
}) {
  return (
    <Context.Provider value={useMemo(() => ({gap}), [gap])}>
      <View
        style={[
          a.flex_row,
          a.flex_1,
          {
            marginLeft: -gap / 2,
            marginRight: -gap / 2,
          },
          style,
        ]}>
        {children}
      </View>
    </Context.Provider>
  )
}

export function Col({
  children,
  width = 1,
  style,
}: ViewStyleProp & {
  children: React.ReactNode
  width?: number
}) {
  const {gap} = useContext(Context)
  return (
    <View
      style={[
        a.flex_col,
        {
          paddingLeft: gap / 2,
          paddingRight: gap / 2,
          width: `${width * 100}%`,
        },
        style,
      ]}>
      {children}
    </View>
  )
}