Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SwipeableDrawer] Add appearOffset prop #30763

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
58c2aef
Merge remote-tracking branch 'upstream/master' into discovery-amount
tech-meppem Jan 24, 2022
0674c0d
Added discovery amount option
tech-meppem Jan 24, 2022
895331a
Fix incorrect double invserse
tech-meppem Jan 25, 2022
5552c0c
Merge remote-tracking branch 'upstream/master' into discovery-amount
tech-meppem Jan 25, 2022
e1d3513
Added demo for discoveryAmount
tech-meppem Jan 25, 2022
c8b12c6
Merge remote-tracking branch 'upstream/master' into discovery-amount
tech-meppem Feb 8, 2022
52dace7
migration pr fix
tech-meppem Feb 8, 2022
9faf1bb
Remove translated demos, and added mobile view clarification.
tech-meppem Feb 8, 2022
b8e2946
fix for discoveryAmount demo files
tech-meppem Feb 8, 2022
4bc395a
Rename to appearOffset
tech-meppem Feb 14, 2022
78fafdd
Merge remote-tracking branch 'upstream/master' into discovery-amount
tech-meppem Feb 14, 2022
1f025f7
lint fixes
tech-meppem Feb 14, 2022
6bf4849
Merge remote-tracking branch 'upstream/master' into discovery-amount
tech-meppem Feb 28, 2022
687b22f
Merge remote-tracking branch 'upstream/master' into discovery-amount
tech-meppem Mar 17, 2022
6e5e936
Merge remote-tracking branch 'upstream/master' into discovery-amount
tech-meppem Mar 22, 2022
e68654d
Merge remote-tracking branch 'upstream/master' into discovery-amount
tech-meppem Apr 19, 2022
fcfcd13
Merge remote-tracking branch 'upstream/master' into discovery-amount
tech-meppem May 27, 2022
c37f0af
Merge remote-tracking branch 'upstream/master' into discovery-amount
tech-meppem Aug 30, 2022
7cdf04b
Merge branch 'master' into discovery-amount
tech-meppem Sep 28, 2022
85171b5
Merge commit '4265f343b49e4af9673a49e585439d35c8deeab2' into discover…
tech-meppem Jan 3, 2023
0cb3d90
Merge commit '191acda4545c394b78964ab80b364e15686f0e9b' into discover…
tech-meppem Jan 3, 2023
66f8626
Merge commit 'e9d8fa07e1ff979bcaf92837b131cd9e292f0161' into discover…
tech-meppem Jan 3, 2023
b9311f5
Merge remote-tracking branch 'upstream/master' into discovery-amount
tech-meppem Jan 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/pages/api-docs/swipeable-drawer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"type": { "name": "bool" },
"default": "typeof navigator !== 'undefined' && /iPad|iPhone|iPod/.test(navigator.userAgent)"
},
"discoveryAmount": { "type": { "name": "number" }, "default": "disableDiscovery ? 15 : 20" },
"hysteresis": { "type": { "name": "number" }, "default": "0.52" },
"minFlingVelocity": { "type": { "name": "number" }, "default": "450" },
"SwipeAreaProps": { "type": { "name": "object" } },
Expand Down
99 changes: 99 additions & 0 deletions docs/src/pages/components/drawers/SwipeableDiscoveryAmount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import SwipeableDrawer from '@mui/material/SwipeableDrawer';
import Radio from '@mui/material/Radio';
import RadioGroup from '@mui/material/RadioGroup';
import FormControlLabel from '@mui/material/FormControlLabel';
import List from '@mui/material/List';
import Divider from '@mui/material/Divider';
import ListItem from '@mui/material/ListItem';
import ListItemIcon from '@mui/material/ListItemIcon';
import ListItemText from '@mui/material/ListItemText';
import InboxIcon from '@mui/icons-material/MoveToInbox';
import MailIcon from '@mui/icons-material/Mail';

export default function SwipeableDiscoveryAmount() {
const [state, setState] = React.useState({
top: false,
left: false,
bottom: false,
right: false,
});

const [discoveryAmount, setDiscoveryAmount] = React.useState(20);

const toggleDrawer = (anchor, open) => (event) => {
if (
event &&
event.type === 'keydown' &&
(event.key === 'Tab' || event.key === 'Shift')
) {
return;
}

setState({ ...state, [anchor]: open });
};

const list = (anchor) => (
<Box
sx={{ width: anchor === 'top' || anchor === 'bottom' ? 'auto' : 250 }}
role="presentation"
onClick={toggleDrawer(anchor, false)}
onKeyDown={toggleDrawer(anchor, false)}
>
<List>
{['Inbox', 'Starred', 'Send email', 'Drafts'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>
{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
<Divider />
<List>
{['All mail', 'Trash', 'Spam'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>
{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
</Box>
);

return (
<div>
<RadioGroup
row
name="justifyContent"
aria-label="justifyContent"
value={discoveryAmount}
onChange={(event) => {
setDiscoveryAmount(parseInt(event.target.value, 10));
}}
>
<FormControlLabel value={0} control={<Radio />} label="0px" />
<FormControlLabel value={20} control={<Radio />} label="20px" />
<FormControlLabel value={50} control={<Radio />} label="50px" />
<FormControlLabel value={100} control={<Radio />} label="100px" />
</RadioGroup>
{['left', 'right', 'top', 'bottom'].map((anchor) => (
<React.Fragment key={anchor}>
<SwipeableDrawer
discoveryAmount={discoveryAmount}
anchor={anchor}
open={state[anchor]}
onClose={toggleDrawer(anchor, false)}
onOpen={toggleDrawer(anchor, true)}
>
{list(anchor)}
</SwipeableDrawer>
</React.Fragment>
))}
</div>
);
}
103 changes: 103 additions & 0 deletions docs/src/pages/components/drawers/SwipeableDiscoveryAmount.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import SwipeableDrawer from '@mui/material/SwipeableDrawer';
import Radio from '@mui/material/Radio';
import RadioGroup from '@mui/material/RadioGroup';
import FormControlLabel from '@mui/material/FormControlLabel';
import List from '@mui/material/List';
import Divider from '@mui/material/Divider';
import ListItem from '@mui/material/ListItem';
import ListItemIcon from '@mui/material/ListItemIcon';
import ListItemText from '@mui/material/ListItemText';
import InboxIcon from '@mui/icons-material/MoveToInbox';
import MailIcon from '@mui/icons-material/Mail';

type Anchor = 'top' | 'left' | 'bottom' | 'right';

export default function SwipeableDiscoveryAmount() {
const [state, setState] = React.useState({
top: false,
left: false,
bottom: false,
right: false,
});
const [discoveryAmount, setDiscoveryAmount] = React.useState(20);

const toggleDrawer =
(anchor: Anchor, open: boolean) =>
(event: React.KeyboardEvent | React.MouseEvent) => {
if (
event &&
event.type === 'keydown' &&
((event as React.KeyboardEvent).key === 'Tab' ||
(event as React.KeyboardEvent).key === 'Shift')
) {
return;
}

setState({ ...state, [anchor]: open });
};

const list = (anchor: Anchor) => (
<Box
sx={{ width: anchor === 'top' || anchor === 'bottom' ? 'auto' : 250 }}
role="presentation"
onClick={toggleDrawer(anchor, false)}
onKeyDown={toggleDrawer(anchor, false)}
>
<List>
{['Inbox', 'Starred', 'Send email', 'Drafts'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>
{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
<Divider />
<List>
{['All mail', 'Trash', 'Spam'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>
{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
</Box>
);

return (
<div>
<RadioGroup
row
name="justifyContent"
aria-label="justifyContent"
value={discoveryAmount}
onChange={(event) => {
setDiscoveryAmount(parseInt((event.target as HTMLInputElement).value, 10));
}}
>
<FormControlLabel value={0} control={<Radio />} label="0px" />
<FormControlLabel value={20} control={<Radio />} label="20px" />
<FormControlLabel value={50} control={<Radio />} label="50px" />
<FormControlLabel value={100} control={<Radio />} label="100px" />
</RadioGroup>
{(['left', 'right', 'top', 'bottom'] as const).map((anchor) => (
<React.Fragment key={anchor}>
<SwipeableDrawer
discoveryAmount={discoveryAmount}
anchor={anchor}
open={state[anchor]}
onClose={toggleDrawer(anchor, false)}
onOpen={toggleDrawer(anchor, true)}
>
{list(anchor)}
</SwipeableDrawer>
</React.Fragment>
))}
</div>
);
}
10 changes: 10 additions & 0 deletions docs/src/pages/components/drawers/drawers-pt.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ const iOS =
<SwipeableDrawer disableBackdropTransition={!iOS} disableDiscovery={iOS} />;
```

### Drawer Discovery

You can make the drawer appear slightly when the user touches the screen near where the drawer is located.

The amount the drawer appears by can be configured using `discoveryAmount`, and it can be disabled using `disableDiscovery`.

If you are on mobile, you can click near the edge of the screen to discover the drawers.

{{"demo": "pages/components/drawers/SwipeableDiscoveryAmount.js"}}

### Borda deslizável

Você pode configurar a propriedade `SwipeableDrawer` para visualizar uma borda quando o Drawer estiver fechado.
Expand Down
10 changes: 10 additions & 0 deletions docs/src/pages/components/drawers/drawers-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ const iOS =
<SwipeableDrawer disableBackdropTransition={!iOS} disableDiscovery={iOS} />;
```

### Drawer Discovery
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only need to edit drawers.md and this file will be translated later.


You can make the drawer appear slightly when the user touches the screen near where the drawer is located.

The amount the drawer appears by can be configured using `discoveryAmount`, and it can be disabled using `disableDiscovery`.

If you are on mobile, you can click near the edge of the screen to discover the drawers.

{{"demo": "pages/components/drawers/SwipeableDiscoveryAmount.js"}}

### 保持挂载(mounted)

你可以使用 `ModalProps` 属性来确保临时抽屉不会被卸载,就像这样:
Expand Down
10 changes: 10 additions & 0 deletions docs/src/pages/components/drawers/drawers.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ const iOS =
<SwipeableDrawer disableBackdropTransition={!iOS} disableDiscovery={iOS} />;
```

### Drawer Discovery

You can make the drawer appear slightly when the user touches the screen near where the drawer is located.

The amount the drawer appears by can be configured using `discoveryAmount`, and it can be disabled using `disableDiscovery`.

If you are on mobile, you can click near the edge of the screen to discover the drawers.

{{"demo": "pages/components/drawers/SwipeableDiscoveryAmount.js"}}

Copy link
Member

@siriwatknp siriwatknp Feb 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a note that the demo only works on the mobile viewport? Also, I try to play this feature in iOS chrome but it does not work (Swipe on the left seems to navigate to the previous page instead).

Copy link
Contributor Author

@tech-meppem tech-meppem Feb 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that's because disableDiscovery is disabled by default on iOS, so it wont work.
I can set it to always be enabled if you think that's better, but that might impact expected navigation on the page itself.

### Swipeable edge

You can configure the `SwipeableDrawer` to have a visible edge when closed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"disableBackdropTransition": "Disable the backdrop transition. This can improve the FPS on low-end devices.",
"disableDiscovery": "If <code>true</code>, touching the screen near the edge of the drawer will not slide in the drawer a bit to promote accidental discovery of the swipe gesture.",
"disableSwipeToOpen": "If <code>true</code>, swipe to open is disabled. This is useful in browsers where swiping triggers navigation actions. Swipe to open is disabled on iOS browsers by default.",
"discoveryAmount": "The amount that the drawer adjusts on initial touch on the swipe area.",
"hysteresis": "Affects how far the drawer must be opened/closed to change its state. Specified as percent (0-1) of the width of the drawer",
"minFlingVelocity": "Defines, from which (average) velocity on, the swipe is defined as complete although hysteresis isn&#39;t reached. Good threshold is between 250 - 1000 px/s",
"onClose": "Callback fired when the component requests to be closed.<br><br><strong>Signature:</strong><br><code>function(event: object) =&gt; void</code><br><em>event:</em> The event source of the callback.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"disableBackdropTransition": "Disable the backdrop transition. This can improve the FPS on low-end devices.",
"disableDiscovery": "If <code>true</code>, touching the screen near the edge of the drawer will not slide in the drawer a bit to promote accidental discovery of the swipe gesture.",
"disableSwipeToOpen": "If <code>true</code>, swipe to open is disabled. This is useful in browsers where swiping triggers navigation actions. Swipe to open is disabled on iOS browsers by default.",
"discoveryAmount": "The amount that the drawer adjusts on initial touch on the swipe area.",
"hysteresis": "Affects how far the drawer must be opened/closed to change its state. Specified as percent (0-1) of the width of the drawer",
"minFlingVelocity": "Defines, from which (average) velocity on, the swipe is defined as complete although hysteresis isn&#39;t reached. Good threshold is between 250 - 1000 px/s",
"onClose": "Callback fired when the component requests to be closed.<br><br><strong>Signature:</strong><br><code>function(event: object) =&gt; void</code><br><em>event:</em> The event source of the callback.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"disableBackdropTransition": "Disable the backdrop transition. This can improve the FPS on low-end devices.",
"disableDiscovery": "If <code>true</code>, touching the screen near the edge of the drawer will not slide in the drawer a bit to promote accidental discovery of the swipe gesture.",
"disableSwipeToOpen": "If <code>true</code>, swipe to open is disabled. This is useful in browsers where swiping triggers navigation actions. Swipe to open is disabled on iOS browsers by default.",
"discoveryAmount": "The amount that the drawer adjusts on initial touch on the swipe area.",
"hysteresis": "Affects how far the drawer must be opened/closed to change its state. Specified as percent (0-1) of the width of the drawer",
"minFlingVelocity": "Defines, from which (average) velocity on, the swipe is defined as complete although hysteresis isn&#39;t reached. Good threshold is between 250 - 1000 px/s",
"onClose": "Callback fired when the component requests to be closed.<br><br><strong>Signature:</strong><br><code>function(event: object) =&gt; void</code><br><em>event:</em> The event source of the callback.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface SwipeableDrawerProps extends Omit<DrawerProps, 'onClose' | 'ope
* @default typeof navigator !== 'undefined' && /iPad|iPhone|iPod/.test(navigator.userAgent)
*/
disableSwipeToOpen?: boolean;
/**
* The amount that the drawer adjusts on initial touch on the swipe area.
* @default disableDiscovery ? 15 : 20
*/
discoveryAmount?: number;
/**
* Affects how far the drawer must be opened/closed to change its state.
* Specified as percent (0-1) of the width of the drawer
Expand Down
15 changes: 9 additions & 6 deletions packages/mui-material/src/SwipeableDrawer/SwipeableDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import SwipeArea from './SwipeArea';
// trigger a native scroll.
const UNCERTAINTY_THRESHOLD = 3; // px

// This is the part of the drawer displayed on touch start.
const DRAG_STARTED_SIGNAL = 20; // px

// We can only have one instance at the time claiming ownership for handling the swipe.
// Otherwise, the UX would be confusing.
// That's why we use a singleton here.
Expand Down Expand Up @@ -141,6 +138,7 @@ const SwipeableDrawer = React.forwardRef(function SwipeableDrawer(inProps, ref)
disableBackdropTransition = false,
disableDiscovery = false,
disableSwipeToOpen = iOS,
discoveryAmount = disableDiscovery ? 15 : 20,
hideBackdrop,
hysteresis = 0.52,
minFlingVelocity = 450,
Expand Down Expand Up @@ -365,9 +363,9 @@ const SwipeableDrawer = React.forwardRef(function SwipeableDrawer(inProps, ref)
// Compensate for the part of the drawer displayed on touch start.
if (!disableDiscovery && !open) {
if (horizontalSwipe) {
swipeInstance.current.startX -= DRAG_STARTED_SIGNAL;
swipeInstance.current.startX -= discoveryAmount;
} else {
swipeInstance.current.startY -= DRAG_STARTED_SIGNAL;
swipeInstance.current.startY -= discoveryAmount;
}
}
}
Expand Down Expand Up @@ -491,7 +489,7 @@ const SwipeableDrawer = React.forwardRef(function SwipeableDrawer(inProps, ref)
// The ref may be null when a parent component updates while swiping.
setPosition(
getMaxTranslate(horizontalSwipe, paperRef.current) +
(disableDiscovery ? 15 : -DRAG_STARTED_SIGNAL),
(disableDiscovery ? discoveryAmount : -discoveryAmount),
{
changeTransition: false,
},
Expand Down Expand Up @@ -614,6 +612,11 @@ SwipeableDrawer.propTypes /* remove-proptypes */ = {
* @default typeof navigator !== 'undefined' && /iPad|iPhone|iPod/.test(navigator.userAgent)
*/
disableSwipeToOpen: PropTypes.bool,
/**
* The amount that the drawer adjusts on initial touch on the swipe area.
* @default disableDiscovery ? 15 : 20
*/
discoveryAmount: PropTypes.number,
/**
* @ignore
*/
Expand Down