forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AspectRatio.tsx
206 lines (193 loc) · 6.29 KB
/
AspectRatio.tsx
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import * as React from 'react';
import PropTypes from 'prop-types';
import { unstable_composeClasses as composeClasses } from '@mui/base';
import { OverridableComponent } from '@mui/types';
import { unstable_capitalize as capitalize } from '@mui/utils';
import useThemeProps from '../styles/useThemeProps';
import useSlot from '../utils/useSlot';
import styled from '../styles/styled';
import { getAspectRatioUtilityClass } from './aspectRatioClasses';
import { AspectRatioProps, AspectRatioOwnerState, AspectRatioTypeMap } from './AspectRatioProps';
const useUtilityClasses = (ownerState: AspectRatioOwnerState) => {
const { variant, color } = ownerState;
const slots = {
root: ['root'],
content: [
'content',
variant && `variant${capitalize(variant)}`,
color && `color${capitalize(color)}`,
],
};
return composeClasses(slots, getAspectRatioUtilityClass, {});
};
// Use to control the width of the content, usually in a flexbox row container
const AspectRatioRoot = styled('div', {
name: 'JoyAspectRatio',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})<{ ownerState: AspectRatioOwnerState }>(({ ownerState }) => {
const minHeight =
typeof ownerState.minHeight === 'number' ? `${ownerState.minHeight}px` : ownerState.minHeight;
const maxHeight =
typeof ownerState.maxHeight === 'number' ? `${ownerState.maxHeight}px` : ownerState.maxHeight;
return {
// a context variable for any child component
'--AspectRatio-paddingBottom':
minHeight || maxHeight
? `clamp(${minHeight || '0px'}, calc(100% / (${ownerState.ratio})), ${
maxHeight || '9999px'
})`
: `calc(100% / (${ownerState.ratio}))`,
borderRadius: 'var(--AspectRatio-radius)',
flexDirection: 'column',
margin: 'var(--AspectRatio-margin)',
};
});
const AspectRatioContent = styled('div', {
name: 'JoyAspectRatio',
slot: 'Content',
overridesResolver: (props, styles) => styles.content,
})<{ ownerState: AspectRatioOwnerState }>(({ theme, ownerState }) => [
{
flex: 1,
position: 'relative',
borderRadius: 'inherit',
height: 0,
paddingBottom: 'var(--AspectRatio-paddingBottom)',
overflow: 'hidden',
// use data-attribute instead of :first-child to support zero config SSR (emotion)
// use nested selector for integrating with nextjs image `fill` layout (spans are inserted on top of the img)
'& [data-first-child]': {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
boxSizing: 'border-box',
position: 'absolute',
width: '100%',
height: '100%',
objectFit: ownerState.objectFit,
margin: 0,
padding: 0,
'& > img': {
// support art-direction that uses <picture><img /></picture>
width: '100%',
height: '100%',
objectFit: ownerState.objectFit,
},
},
},
theme.variants[ownerState.variant!]?.[ownerState.color!],
]);
const AspectRatio = React.forwardRef(function AspectRatio(inProps, ref) {
const props = useThemeProps<typeof inProps & AspectRatioProps>({
props: inProps,
name: 'JoyAspectRatio',
});
const {
children,
ratio = '16 / 9',
minHeight,
maxHeight,
objectFit = 'cover',
color = 'neutral',
variant = 'soft',
...other
} = props;
const ownerState = {
...props,
minHeight,
maxHeight,
objectFit,
ratio,
color,
variant,
};
const classes = useUtilityClasses(ownerState);
const [SlotRoot, rootProps] = useSlot('root', {
ref,
className: classes.root,
elementType: AspectRatioRoot,
externalForwardedProps: other,
ownerState,
});
const [SlotContent, contentProps] = useSlot('content', {
className: classes.content,
elementType: AspectRatioContent,
externalForwardedProps: other,
ownerState,
});
return (
<SlotRoot {...rootProps}>
<SlotContent {...contentProps}>
{React.Children.map(children, (child, index) =>
index === 0 && React.isValidElement(child)
? React.cloneElement(child, { 'data-first-child': '' } as Record<string, string>)
: child,
)}
</SlotContent>
</SlotRoot>
);
}) as OverridableComponent<AspectRatioTypeMap>;
AspectRatio.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* Used to render icon or text elements inside the AspectRatio if `src` is not set.
* This can be an element, or just a string.
*/
children: PropTypes.node,
/**
* The color of the component. It supports those theme colors that make sense for this component.
* @default 'neutral'
*/
color: PropTypes.oneOf(['danger', 'info', 'neutral', 'primary', 'success', 'warning']),
/**
* The maximum calculated height of the element (not the CSS height).
*/
maxHeight: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/**
* The minimum calculated height of the element (not the CSS height).
*/
minHeight: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/**
* The CSS object-fit value of the first-child.
*/
objectFit: PropTypes.oneOf([
'-moz-initial',
'contain',
'cover',
'fill',
'inherit',
'initial',
'none',
'revert-layer',
'revert',
'scale-down',
'unset',
]),
/**
* The aspect-ratio of the element. The current implementation uses padding instead of the CSS aspect-ratio due to browser support.
* https://caniuse.com/?search=aspect-ratio
* @default '16 / 9'
*/
ratio: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/**
* The system prop that 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,
]),
/**
* The variant to use.
* @default 'soft'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['outlined', 'plain', 'soft', 'solid']),
PropTypes.string,
]),
} as any;
export default AspectRatio;