193 lines
No EOL
5 KiB
JavaScript
193 lines
No EOL
5 KiB
JavaScript
import { traverseBreakpoints } from "./traverseBreakpoints.js";
|
|
function getSelfSpacingVar(axis) {
|
|
return `--Grid-${axis}Spacing`;
|
|
}
|
|
function getParentSpacingVar(axis) {
|
|
return `--Grid-parent-${axis}Spacing`;
|
|
}
|
|
const selfColumnsVar = '--Grid-columns';
|
|
const parentColumnsVar = '--Grid-parent-columns';
|
|
export const generateGridSizeStyles = ({
|
|
theme,
|
|
ownerState
|
|
}) => {
|
|
const styles = {};
|
|
traverseBreakpoints(theme.breakpoints, ownerState.size, (appendStyle, value) => {
|
|
let style = {};
|
|
if (value === 'grow') {
|
|
style = {
|
|
flexBasis: 0,
|
|
flexGrow: 1,
|
|
maxWidth: '100%'
|
|
};
|
|
}
|
|
if (value === 'auto') {
|
|
style = {
|
|
flexBasis: 'auto',
|
|
flexGrow: 0,
|
|
flexShrink: 0,
|
|
maxWidth: 'none',
|
|
width: 'auto'
|
|
};
|
|
}
|
|
if (typeof value === 'number') {
|
|
style = {
|
|
flexGrow: 0,
|
|
flexBasis: 'auto',
|
|
width: `calc(100% * ${value} / var(${parentColumnsVar}) - (var(${parentColumnsVar}) - ${value}) * (var(${getParentSpacingVar('column')}) / var(${parentColumnsVar})))`
|
|
};
|
|
}
|
|
appendStyle(styles, style);
|
|
});
|
|
return styles;
|
|
};
|
|
export const generateGridOffsetStyles = ({
|
|
theme,
|
|
ownerState
|
|
}) => {
|
|
const styles = {};
|
|
traverseBreakpoints(theme.breakpoints, ownerState.offset, (appendStyle, value) => {
|
|
let style = {};
|
|
if (value === 'auto') {
|
|
style = {
|
|
marginLeft: 'auto'
|
|
};
|
|
}
|
|
if (typeof value === 'number') {
|
|
style = {
|
|
marginLeft: value === 0 ? '0px' : `calc(100% * ${value} / var(${parentColumnsVar}) + var(${getParentSpacingVar('column')}) * ${value} / var(${parentColumnsVar}))`
|
|
};
|
|
}
|
|
appendStyle(styles, style);
|
|
});
|
|
return styles;
|
|
};
|
|
export const generateGridColumnsStyles = ({
|
|
theme,
|
|
ownerState
|
|
}) => {
|
|
if (!ownerState.container) {
|
|
return {};
|
|
}
|
|
const styles = {
|
|
[selfColumnsVar]: 12
|
|
};
|
|
traverseBreakpoints(theme.breakpoints, ownerState.columns, (appendStyle, value) => {
|
|
const columns = value ?? 12;
|
|
appendStyle(styles, {
|
|
[selfColumnsVar]: columns,
|
|
'> *': {
|
|
[parentColumnsVar]: columns
|
|
}
|
|
});
|
|
});
|
|
return styles;
|
|
};
|
|
export const generateGridRowSpacingStyles = ({
|
|
theme,
|
|
ownerState
|
|
}) => {
|
|
if (!ownerState.container) {
|
|
return {};
|
|
}
|
|
const styles = {};
|
|
traverseBreakpoints(theme.breakpoints, ownerState.rowSpacing, (appendStyle, value) => {
|
|
const spacing = typeof value === 'string' ? value : theme.spacing?.(value);
|
|
appendStyle(styles, {
|
|
[getSelfSpacingVar('row')]: spacing,
|
|
'> *': {
|
|
[getParentSpacingVar('row')]: spacing
|
|
}
|
|
});
|
|
});
|
|
return styles;
|
|
};
|
|
export const generateGridColumnSpacingStyles = ({
|
|
theme,
|
|
ownerState
|
|
}) => {
|
|
if (!ownerState.container) {
|
|
return {};
|
|
}
|
|
const styles = {};
|
|
traverseBreakpoints(theme.breakpoints, ownerState.columnSpacing, (appendStyle, value) => {
|
|
const spacing = typeof value === 'string' ? value : theme.spacing?.(value);
|
|
appendStyle(styles, {
|
|
[getSelfSpacingVar('column')]: spacing,
|
|
'> *': {
|
|
[getParentSpacingVar('column')]: spacing
|
|
}
|
|
});
|
|
});
|
|
return styles;
|
|
};
|
|
export const generateGridDirectionStyles = ({
|
|
theme,
|
|
ownerState
|
|
}) => {
|
|
if (!ownerState.container) {
|
|
return {};
|
|
}
|
|
const styles = {};
|
|
traverseBreakpoints(theme.breakpoints, ownerState.direction, (appendStyle, value) => {
|
|
appendStyle(styles, {
|
|
flexDirection: value
|
|
});
|
|
});
|
|
return styles;
|
|
};
|
|
export const generateGridStyles = ({
|
|
ownerState
|
|
}) => {
|
|
return {
|
|
minWidth: 0,
|
|
boxSizing: 'border-box',
|
|
...(ownerState.container && {
|
|
display: 'flex',
|
|
flexWrap: 'wrap',
|
|
...(ownerState.wrap && ownerState.wrap !== 'wrap' && {
|
|
flexWrap: ownerState.wrap
|
|
}),
|
|
gap: `var(${getSelfSpacingVar('row')}) var(${getSelfSpacingVar('column')})`
|
|
})
|
|
};
|
|
};
|
|
export const generateSizeClassNames = size => {
|
|
const classNames = [];
|
|
Object.entries(size).forEach(([key, value]) => {
|
|
if (value !== false && value !== undefined) {
|
|
classNames.push(`grid-${key}-${String(value)}`);
|
|
}
|
|
});
|
|
return classNames;
|
|
};
|
|
export const generateSpacingClassNames = (spacing, smallestBreakpoint = 'xs') => {
|
|
function isValidSpacing(val) {
|
|
if (val === undefined) {
|
|
return false;
|
|
}
|
|
return typeof val === 'string' && !Number.isNaN(Number(val)) || typeof val === 'number' && val > 0;
|
|
}
|
|
if (isValidSpacing(spacing)) {
|
|
return [`spacing-${smallestBreakpoint}-${String(spacing)}`];
|
|
}
|
|
if (typeof spacing === 'object' && !Array.isArray(spacing)) {
|
|
const classNames = [];
|
|
Object.entries(spacing).forEach(([key, value]) => {
|
|
if (isValidSpacing(value)) {
|
|
classNames.push(`spacing-${key}-${String(value)}`);
|
|
}
|
|
});
|
|
return classNames;
|
|
}
|
|
return [];
|
|
};
|
|
export const generateDirectionClasses = direction => {
|
|
if (direction === undefined) {
|
|
return [];
|
|
}
|
|
if (typeof direction === 'object') {
|
|
return Object.entries(direction).map(([key, value]) => `direction-${key}-${value}`);
|
|
}
|
|
return [`direction-xs-${String(direction)}`];
|
|
}; |