Skip to content

Commit

Permalink
container seem to closer match the terminology of the use case
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Aug 8, 2021
1 parent 675c971 commit 62eb2a5
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 137 deletions.
4 changes: 3 additions & 1 deletion docs/pages/api-docs/slide.json
@@ -1,8 +1,10 @@
{
"props": {
"anchorEl": { "type": { "name": "union", "description": "HTML element<br>&#124;&nbsp;func" } },
"appear": { "type": { "name": "bool" }, "default": "true" },
"children": { "type": { "name": "custom", "description": "element" } },
"container": {
"type": { "name": "custom", "description": "HTML element<br>&#124;&nbsp;func" }
},
"direction": {
"type": {
"name": "enum",
Expand Down
Expand Up @@ -21,9 +21,9 @@ const icon = (
</Paper>
);

export default function SlideFromAnchorEl() {
export default function SlideFromContainer() {
const [checked, setChecked] = React.useState(false);
const anchorRef = React.useRef(null);
const containerRef = React.useRef(null);

const handleChange = () => {
setChecked((prev) => !prev);
Expand All @@ -41,20 +41,14 @@ export default function SlideFromAnchorEl() {
theme.palette.mode === 'light' ? 'grey.100' : 'grey.900',
overflow: 'hidden',
}}
ref={anchorRef}
ref={containerRef}
>
<Box sx={{ width: 200 }}>
<FormControlLabel
control={<Switch checked={checked} onChange={handleChange} />}
label="Show from target"
/>
<Slide
direction="up"
in={checked}
anchorEl={anchorRef.current}
mountOnEnter
unmountOnExit
>
<Slide direction="up" in={checked} container={containerRef.current}>
{icon}
</Slide>
</Box>
Expand Down
Expand Up @@ -21,9 +21,9 @@ const icon = (
</Paper>
);

export default function SlideFromAnchorEl() {
export default function SlideFromContainer() {
const [checked, setChecked] = React.useState(false);
const anchorRef = React.useRef(null);
const containerRef = React.useRef(null);

const handleChange = () => {
setChecked((prev) => !prev);
Expand All @@ -41,20 +41,14 @@ export default function SlideFromAnchorEl() {
theme.palette.mode === 'light' ? 'grey.100' : 'grey.900',
overflow: 'hidden',
}}
ref={anchorRef}
ref={containerRef}
>
<Box sx={{ width: 200 }}>
<FormControlLabel
control={<Switch checked={checked} onChange={handleChange} />}
label="Show from target"
/>
<Slide
direction="up"
in={checked}
anchorEl={anchorRef.current}
mountOnEnter
unmountOnExit
>
<Slide direction="up" in={checked} container={containerRef.current}>
{icon}
</Slide>
</Box>
Expand Down
6 changes: 3 additions & 3 deletions docs/src/pages/components/transitions/transitions.md
Expand Up @@ -48,12 +48,12 @@ Similarly, the `unmountOnExit` prop removes the component from the DOM after it

{{"demo": "pages/components/transitions/SimpleSlide.js", "bg": true}}

### Slide from anchor DOM node
### Slide relative to a container

The Slide component also accepts `anchorEl` prop, which is a reference to a DOM node.
The Slide component also accepts `container` prop, which is a reference to a DOM node.
If this prop is set, the Slide component will slide from the edge of that DOM node.

{{"demo": "pages/components/transitions/SlideFromAnchorEl.js"}}
{{"demo": "pages/components/transitions/SlideFromContainer.js"}}

## Zoom

Expand Down
2 changes: 1 addition & 1 deletion docs/translations/api-docs/slide/slide.json
@@ -1,9 +1,9 @@
{
"componentDescription": "The Slide transition is used by the [Drawer](/components/drawers/) component.\nIt uses [react-transition-group](https://github.com/reactjs/react-transition-group) internally.",
"propDescriptions": {
"anchorEl": "An HTML element, or a function that returns one. It&#39;s used to set the position of the Slide.",
"appear": "Perform the enter transition when it first mounts if <code>in</code> is also <code>true</code>. Set this to <code>false</code> to disable this behavior.",
"children": "A single child content element.<br>⚠️ <a href=\"/guides/composition/#caveat-with-refs\">Needs to be able to hold a ref</a>.",
"container": "An HTML element, or a function that returns one. It&#39;s used to set the position of the Slide.",
"direction": "Direction the child node will enter from.",
"easing": "The transition timing function. You may specify a single easing or a object containing enter and exit values.",
"in": "If <code>true</code>, the component will transition in.",
Expand Down
10 changes: 5 additions & 5 deletions packages/material-ui/src/Popover/Popover.js
Expand Up @@ -54,7 +54,7 @@ function getTransformOriginValue(transformOrigin) {
.join(' ');
}

function getAnchorEl(anchorEl) {
function resolveAnchorEl(anchorEl) {
return typeof anchorEl === 'function' ? anchorEl() : anchorEl;
}

Expand Down Expand Up @@ -153,7 +153,7 @@ const Popover = React.forwardRef(function Popover(inProps, ref) {
return anchorPosition;
}

const resolvedAnchorEl = getAnchorEl(anchorEl);
const resolvedAnchorEl = resolveAnchorEl(anchorEl);

// If an anchor element wasn't provided, just use the parent body element of this Popover
const anchorElement =
Expand Down Expand Up @@ -227,7 +227,7 @@ const Popover = React.forwardRef(function Popover(inProps, ref) {
const right = left + elemRect.width;

// Use the parent window of the anchorEl if provided
const containerWindow = ownerWindow(getAnchorEl(anchorEl));
const containerWindow = ownerWindow(resolveAnchorEl(anchorEl));

// Window thresholds taking required margin into account
const heightThreshold = containerWindow.innerHeight - marginThreshold;
Expand Down Expand Up @@ -350,7 +350,7 @@ const Popover = React.forwardRef(function Popover(inProps, ref) {
// If the anchorEl prop is provided, use its parent body element as the container
// If neither are provided let the Modal take care of choosing the container
const container =
containerProp || (anchorEl ? ownerDocument(getAnchorEl(anchorEl)).body : undefined);
containerProp || (anchorEl ? ownerDocument(resolveAnchorEl(anchorEl)).body : undefined);

return (
<PopoverRoot
Expand Down Expand Up @@ -398,7 +398,7 @@ Popover.propTypes /* remove-proptypes */ = {
*/
anchorEl: chainPropTypes(PropTypes.oneOfType([HTMLElementType, PropTypes.func]), (props) => {
if (props.open && (!props.anchorReference || props.anchorReference === 'anchorEl')) {
const resolvedAnchorEl = getAnchorEl(props.anchorEl);
const resolvedAnchorEl = resolveAnchorEl(props.anchorEl);

if (resolvedAnchorEl && resolvedAnchorEl.nodeType === 1) {
const box = resolvedAnchorEl.getBoundingClientRect();
Expand Down
10 changes: 5 additions & 5 deletions packages/material-ui/src/Popper/Popper.js
Expand Up @@ -29,7 +29,7 @@ function flipPlacement(placement, theme) {
}
}

function getAnchorEl(anchorEl) {
function resolveAnchorEl(anchorEl) {
return typeof anchorEl === 'function' ? anchorEl() : anchorEl;
}

Expand Down Expand Up @@ -84,7 +84,7 @@ const PopperTooltip = React.forwardRef(function PopperTooltip(props, ref) {
setPlacement(data.placement);
};

const resolvedAnchorEl = getAnchorEl(anchorEl);
const resolvedAnchorEl = resolveAnchorEl(anchorEl);

if (process.env.NODE_ENV !== 'production') {
if (resolvedAnchorEl && resolvedAnchorEl.nodeType === 1) {
Expand Down Expand Up @@ -138,7 +138,7 @@ const PopperTooltip = React.forwardRef(function PopperTooltip(props, ref) {
popperModifiers = popperModifiers.concat(popperOptions.modifiers);
}

const popper = createPopper(getAnchorEl(anchorEl), tooltipRef.current, {
const popper = createPopper(resolveAnchorEl(anchorEl), tooltipRef.current, {
placement: rtlPlacement,
...popperOptions,
modifiers: popperModifiers,
Expand Down Expand Up @@ -204,7 +204,7 @@ const Popper = React.forwardRef(function Popper(props, ref) {
// If the anchorEl prop is provided, use its parent body element as the container
// If neither are provided let the Modal take care of choosing the container
const container =
containerProp || (anchorEl ? ownerDocument(getAnchorEl(anchorEl)).body : undefined);
containerProp || (anchorEl ? ownerDocument(resolveAnchorEl(anchorEl)).body : undefined);

return (
<Portal disablePortal={disablePortal} container={container}>
Expand Down Expand Up @@ -258,7 +258,7 @@ Popper.propTypes /* remove-proptypes */ = {
PropTypes.oneOfType([HTMLElementType, PropTypes.object, PropTypes.func]),
(props) => {
if (props.open) {
const resolvedAnchorEl = getAnchorEl(props.anchorEl);
const resolvedAnchorEl = resolveAnchorEl(props.anchorEl);

if (resolvedAnchorEl && resolvedAnchorEl.nodeType === 1) {
const box = resolvedAnchorEl.getBoundingClientRect();
Expand Down
10 changes: 5 additions & 5 deletions packages/material-ui/src/Slide/Slide.d.ts
Expand Up @@ -12,6 +12,11 @@ export interface SlideProps extends TransitionProps {
* A single child content element.
*/
children?: React.ReactElement<any, any>;
/**
* An HTML element, or a function that returns one.
* It's used to set the position of the Slide.
*/
container?: null | Element | ((element: Element) => Element);
/**
* Direction the child node will enter from.
* @default 'down'
Expand All @@ -31,11 +36,6 @@ export interface SlideProps extends TransitionProps {
*/
in?: TransitionProps['in'];
ref?: React.Ref<unknown>;
/**
* An HTML element, or a function that returns one.
* It's used to set the position of the Slide.
*/
anchorEl?: null | Element | ((element: Element) => Element);
/**
* The duration for the transition, in milliseconds.
* You may specify a single timeout for all transitions, or individually with an object.
Expand Down

0 comments on commit 62eb2a5

Please sign in to comment.