|
1 |
| -import * as React from 'react'; |
2 | 1 | import PropTypes from 'prop-types';
|
3 |
| -import { |
4 |
| - createUnarySpacing, |
5 |
| - getValue, |
6 |
| - handleBreakpoints, |
7 |
| - mergeBreakpointsInOrder, |
8 |
| - unstable_extendSxProp as extendSxProp, |
9 |
| - unstable_resolveBreakpointValues as resolveBreakpointValues, |
10 |
| -} from '@mui/system'; |
11 |
| -import { deepmerge } from '@mui/utils'; |
| 2 | +import { createStack } from '@mui/system'; |
12 | 3 | import styled from '../styles/styled';
|
13 | 4 | import useThemeProps from '../styles/useThemeProps';
|
14 | 5 |
|
15 |
| -/** |
16 |
| - * Return an array with the separator React element interspersed between |
17 |
| - * each React node of the input children. |
18 |
| - * |
19 |
| - * > joinChildren([1,2,3], 0) |
20 |
| - * [1,0,2,0,3] |
21 |
| - */ |
22 |
| -function joinChildren(children, separator) { |
23 |
| - const childrenArray = React.Children.toArray(children).filter(Boolean); |
24 |
| - |
25 |
| - return childrenArray.reduce((output, child, index) => { |
26 |
| - output.push(child); |
27 |
| - |
28 |
| - if (index < childrenArray.length - 1) { |
29 |
| - output.push(React.cloneElement(separator, { key: `separator-${index}` })); |
30 |
| - } |
31 |
| - |
32 |
| - return output; |
33 |
| - }, []); |
34 |
| -} |
35 |
| - |
36 |
| -const getSideFromDirection = (direction) => { |
37 |
| - return { |
38 |
| - row: 'Left', |
39 |
| - 'row-reverse': 'Right', |
40 |
| - column: 'Top', |
41 |
| - 'column-reverse': 'Bottom', |
42 |
| - }[direction]; |
43 |
| -}; |
44 |
| - |
45 |
| -export const style = ({ ownerState, theme }) => { |
46 |
| - let styles = { |
47 |
| - display: 'flex', |
48 |
| - flexDirection: 'column', |
49 |
| - ...handleBreakpoints( |
50 |
| - { theme }, |
51 |
| - resolveBreakpointValues({ |
52 |
| - values: ownerState.direction, |
53 |
| - breakpoints: theme.breakpoints.values, |
54 |
| - }), |
55 |
| - (propValue) => ({ |
56 |
| - flexDirection: propValue, |
57 |
| - }), |
58 |
| - ), |
59 |
| - }; |
60 |
| - |
61 |
| - if (ownerState.spacing) { |
62 |
| - const transformer = createUnarySpacing(theme); |
63 |
| - |
64 |
| - const base = Object.keys(theme.breakpoints.values).reduce((acc, breakpoint) => { |
65 |
| - if ( |
66 |
| - (typeof ownerState.spacing === 'object' && ownerState.spacing[breakpoint] != null) || |
67 |
| - (typeof ownerState.direction === 'object' && ownerState.direction[breakpoint] != null) |
68 |
| - ) { |
69 |
| - acc[breakpoint] = true; |
70 |
| - } |
71 |
| - return acc; |
72 |
| - }, {}); |
73 |
| - |
74 |
| - const directionValues = resolveBreakpointValues({ |
75 |
| - values: ownerState.direction, |
76 |
| - base, |
77 |
| - }); |
78 |
| - |
79 |
| - const spacingValues = resolveBreakpointValues({ |
80 |
| - values: ownerState.spacing, |
81 |
| - base, |
82 |
| - }); |
83 |
| - |
84 |
| - if (typeof directionValues === 'object') { |
85 |
| - Object.keys(directionValues).forEach((breakpoint, index, breakpoints) => { |
86 |
| - const directionValue = directionValues[breakpoint]; |
87 |
| - if (!directionValue) { |
88 |
| - const previousDirectionValue = |
89 |
| - index > 0 ? directionValues[breakpoints[index - 1]] : 'column'; |
90 |
| - directionValues[breakpoint] = previousDirectionValue; |
91 |
| - } |
92 |
| - }); |
93 |
| - } |
94 |
| - |
95 |
| - const styleFromPropValue = (propValue, breakpoint) => { |
96 |
| - return { |
97 |
| - '& > :not(style) + :not(style)': { |
98 |
| - margin: 0, |
99 |
| - [`margin${getSideFromDirection( |
100 |
| - breakpoint ? directionValues[breakpoint] : ownerState.direction, |
101 |
| - )}`]: getValue(transformer, propValue), |
102 |
| - }, |
103 |
| - }; |
104 |
| - }; |
105 |
| - styles = deepmerge(styles, handleBreakpoints({ theme }, spacingValues, styleFromPropValue)); |
106 |
| - } |
107 |
| - |
108 |
| - styles = mergeBreakpointsInOrder(theme.breakpoints, styles); |
109 |
| - |
110 |
| - return styles; |
111 |
| -}; |
112 |
| - |
113 |
| -const StackRoot = styled('div', { |
114 |
| - name: 'MuiStack', |
115 |
| - slot: 'Root', |
116 |
| - overridesResolver: (props, styles) => { |
117 |
| - return [styles.root]; |
118 |
| - }, |
119 |
| -})(style); |
120 |
| - |
121 |
| -const Stack = React.forwardRef(function Stack(inProps, ref) { |
122 |
| - const themeProps = useThemeProps({ props: inProps, name: 'MuiStack' }); |
123 |
| - const props = extendSxProp(themeProps); |
124 |
| - const { |
125 |
| - component = 'div', |
126 |
| - direction = 'column', |
127 |
| - spacing = 0, |
128 |
| - divider, |
129 |
| - children, |
130 |
| - ...other |
131 |
| - } = props; |
132 |
| - const ownerState = { |
133 |
| - direction, |
134 |
| - spacing, |
135 |
| - }; |
136 |
| - |
137 |
| - return ( |
138 |
| - <StackRoot as={component} ownerState={ownerState} ref={ref} {...other}> |
139 |
| - {divider ? joinChildren(children, divider) : children} |
140 |
| - </StackRoot> |
141 |
| - ); |
| 6 | +const Stack = createStack({ |
| 7 | + createStyledComponent: styled('div', { |
| 8 | + name: 'MuiStack', |
| 9 | + slot: 'Root', |
| 10 | + overridesResolver: (props, styles) => styles.root, |
| 11 | + }), |
| 12 | + useThemeProps: (inProps) => useThemeProps({ props: inProps, name: 'MuiStack' }), |
142 | 13 | });
|
143 | 14 |
|
144 | 15 | Stack.propTypes /* remove-proptypes */ = {
|
|
0 commit comments