-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Stack.js
174 lines (157 loc) · 4.61 KB
/
Stack.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import * as React from 'react';
import PropTypes from 'prop-types';
import {
createUnarySpacing,
getValue,
handleBreakpoints,
unstable_extendSxProp as extendSxProp,
unstable_resolveBreakpointValues as resolveBreakpointValues,
} from '@mui/system';
import { deepmerge } from '@mui/utils';
import styled from '../styles/styled';
import useThemeProps from '../styles/useThemeProps';
/**
* Return an array with the separator React element interspersed between
* each React node of the input children.
*
* > joinChildren([1,2,3], 0)
* [1,0,2,0,3]
*/
function joinChildren(children, separator) {
const childrenArray = React.Children.toArray(children).filter(Boolean);
return childrenArray.reduce((output, child, index) => {
output.push(child);
if (index < childrenArray.length - 1) {
output.push(React.cloneElement(separator, { key: `separator-${index}` }));
}
return output;
}, []);
}
const getSideFromDirection = (direction) => {
return {
row: 'Left',
'row-reverse': 'Right',
column: 'Top',
'column-reverse': 'Bottom',
}[direction];
};
export const style = ({ ownerState, theme }) => {
let styles = {
display: 'flex',
...handleBreakpoints(
{ theme },
resolveBreakpointValues({
values: ownerState.direction,
breakpoints: theme.breakpoints.values,
}),
(propValue) => ({
flexDirection: propValue,
}),
),
};
if (ownerState.spacing) {
const transformer = createUnarySpacing(theme);
const base = Object.keys(theme.breakpoints.values).reduce((acc, breakpoint) => {
if (ownerState.spacing[breakpoint] != null || ownerState.direction[breakpoint] != null) {
acc[breakpoint] = true;
}
return acc;
}, {});
const directionValues = resolveBreakpointValues({
values: ownerState.direction,
base,
});
const spacingValues = resolveBreakpointValues({
values: ownerState.spacing,
base,
});
const styleFromPropValue = (propValue, breakpoint) => {
return {
'& > :not(style) + :not(style)': {
margin: 0,
[`margin${getSideFromDirection(
breakpoint ? directionValues[breakpoint] : ownerState.direction,
)}`]: getValue(transformer, propValue),
},
};
};
styles = deepmerge(styles, handleBreakpoints({ theme }, spacingValues, styleFromPropValue));
}
return styles;
};
const StackRoot = styled('div', {
name: 'MuiStack',
slot: 'Root',
overridesResolver: (props, styles) => {
return [styles.root];
},
})(style);
const Stack = React.forwardRef(function Stack(inProps, ref) {
const themeProps = useThemeProps({ props: inProps, name: 'MuiStack' });
const props = extendSxProp(themeProps);
const {
component = 'div',
direction = 'column',
spacing = 0,
divider,
children,
...other
} = props;
const ownerState = {
direction,
spacing,
};
return (
<StackRoot as={component} ownerState={ownerState} ref={ref} {...other}>
{divider ? joinChildren(children, divider) : children}
</StackRoot>
);
});
Stack.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* Defines the `flex-direction` style property.
* It is applied for all screen sizes.
* @default 'column'
*/
direction: PropTypes.oneOfType([
PropTypes.oneOf(['column-reverse', 'column', 'row-reverse', 'row']),
PropTypes.arrayOf(PropTypes.oneOf(['column-reverse', 'column', 'row-reverse', 'row'])),
PropTypes.object,
]),
/**
* Add an element between each child.
*/
divider: PropTypes.node,
/**
* Defines the space between immediate children.
* @default 0
*/
spacing: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.number, PropTypes.string])),
PropTypes.number,
PropTypes.object,
PropTypes.string,
]),
/**
* The system prop, which allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
};
export default Stack;