aboutsummaryrefslogtreecommitdiff
path: root/components/utils/systemProps.tsx
blob: 8fc0317d9dc811ccda4966c1867a730381478be1 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import React from 'react';

export type SystemProps = {
  // Margin
  mt?: number;
  mb?: number;
  ml?: number;
  mr?: number;
  mx?: number;
  my?: number;
  m?: number;

  // Padding
  pt?: number;
  pb?: number;
  pl?: number;
  pr?: number;
  px?: number;
  py?: number;
  p?: number;

  // Arbitrary style
  sx?: React.CSSProperties;
};

const calculateStyleValue = (multiplier: number, spacer: number = 1): string => {
  return `${multiplier * spacer}em`;
};

const getSystemStyle = (
  systemProps: SystemProps,
  userStyle: React.CSSProperties = {}
): React.CSSProperties => {
  const spacer = 1; // 1em

  let style: React.CSSProperties = {};

  // Margin
  const { mt, mb, ml, mr, mx, my, m } = systemProps;

  if (m !== undefined) {
    const value = calculateStyleValue(m, spacer);
    style.marginTop = value;
    style.marginBottom = value;
    style.marginLeft = value;
    style.marginRight = value;
  }

  if (mx !== undefined) {
    const value = calculateStyleValue(mx, spacer);
    style.marginLeft = value;
    style.marginRight = value;
  }

  if (my !== undefined) {
    const value = calculateStyleValue(my, spacer);
    style.marginTop = value;
    style.marginBottom = value;
  }

  if (mt !== undefined) style.marginTop = calculateStyleValue(mt, spacer);
  if (mb !== undefined) style.marginBottom = calculateStyleValue(mb, spacer);
  if (ml !== undefined) style.marginLeft = calculateStyleValue(ml, spacer);
  if (mr !== undefined) style.marginRight = calculateStyleValue(mr, spacer);

  // Padding
  const { pt, pb, pl, pr, px, py, p } = systemProps;

  if (p !== undefined) {
    const value = calculateStyleValue(p, spacer);
    style.paddingTop = value;
    style.paddingBottom = value;
    style.paddingLeft = value;
    style.paddingRight = value;
  }

  if (px !== undefined) {
    const value = calculateStyleValue(px, spacer);
    style.paddingLeft = value;
    style.paddingRight = value;
  }

  if (py !== undefined) {
    const value = calculateStyleValue(py, spacer);
    style.paddingTop = value;
    style.paddingBottom = value;
  }

  if (pt !== undefined) style.paddingTop = calculateStyleValue(pt, spacer);
  if (pb !== undefined) style.paddingBottom = calculateStyleValue(pb, spacer);
  if (pl !== undefined) style.paddingLeft = calculateStyleValue(pl, spacer);
  if (pr !== undefined) style.paddingRight = calculateStyleValue(pr, spacer);

  // Merge with arbitrary styles (sx)
  if (systemProps.sx) {
    style = { ...style, ...systemProps.sx };
  }

  // Override with user-defined style
  style = { ...style, ...userStyle };

  return style;
};

export {
  getSystemStyle
};