-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from icflorescu/next
Backport imperative hiding functionality
- Loading branch information
Showing
10 changed files
with
340 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useHotkeys, usePageLeave, useTimeout } from '@mantine/hooks'; | ||
import { useContextMenu } from 'mantine-contextmenu'; | ||
import { useEffect } from 'react'; | ||
import Picture from '~/components/Picture'; | ||
import { copyImageToClipboard, downloadImage, unsplashImages } from '~/lib/image'; | ||
|
||
export default function ImperativeHidingExample() { | ||
const { showContextMenu, hideContextMenu, isContextMenuVisible } = useContextMenu(); | ||
|
||
// 👇 hide the context menu after five seconds have elapsed | ||
const { start: startHiding, clear: cancelHiding } = useTimeout(hideContextMenu, 5000); | ||
useEffect(() => { | ||
if (isContextMenuVisible) { | ||
startHiding(); | ||
} else { | ||
cancelHiding(); | ||
} | ||
}, [cancelHiding, isContextMenuVisible, startHiding]); | ||
|
||
// 👇 hide the context menu when the user hits the `H` key | ||
useHotkeys([['H', hideContextMenu]]); | ||
|
||
// 👇 hide the context menu when the mouse cursor leaves the page | ||
usePageLeave(hideContextMenu); | ||
|
||
// example-skip | ||
const image = unsplashImages[2]; | ||
const { src } = image.file; | ||
// example-resume | ||
return ( | ||
<Picture | ||
image={image} | ||
onContextMenu={showContextMenu([ | ||
{ | ||
key: 'copy', | ||
onClick: () => copyImageToClipboard(src), | ||
}, | ||
{ | ||
key: 'download', | ||
onClick: () => downloadImage(src), | ||
}, | ||
])} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Code, Container } from '@mantine/core'; | ||
import { GetStaticProps, InferGetStaticPropsType } from 'next'; | ||
import CodeBlock from '~/components/CodeBlock'; | ||
import PageNavigation from '~/components/PageNavigation'; | ||
import PageText from '~/components/PageText'; | ||
import PageTitle from '~/components/PageTitle'; | ||
import ImperativeHidingExample from '~/examples/ImperativeHidingExample'; | ||
import readCodeExample from '~/lib/readCodeExample'; | ||
|
||
const PATH = 'examples/imperative-hiding'; | ||
|
||
export const getStaticProps: GetStaticProps<{ code: string }> = async () => ({ | ||
props: { code: (await readCodeExample('examples/ImperativeHidingExample.tsx')) as string }, | ||
}); | ||
|
||
export default function Page({ code }: InferGetStaticPropsType<typeof getStaticProps>) { | ||
return ( | ||
<Container> | ||
<PageTitle of={PATH} /> | ||
<PageText> | ||
A visible context menu hides automatically when the user clicks anywhere on the page, hits the{' '} | ||
<Code>Escape</Code> key, scrolls the or resizes the browser window. | ||
</PageText> | ||
<PageText> | ||
However, you can also hide the context menu <em>imperatively</em> by destructuring the result returned by the{' '} | ||
<Code>useContextMenu</Code> hook into: | ||
</PageText> | ||
<ul> | ||
<li> | ||
<Code>showContextMenu</Code> → a function that can be used to show the context menu; | ||
</li> | ||
<li> | ||
<Code>hideContextMenu</Code> → a function that can be used to hide the context menu; | ||
</li> | ||
<li> | ||
<Code>isContextMenuVisible</Code> → a <Code>boolean</Code> representing whether the context menu is currently | ||
visible or not. | ||
</li> | ||
</ul> | ||
<PageText> | ||
In the example below, we’ll hide the context menu automatically when the user presses the <Code>H</Code> key, | ||
his mouse cursor leaves the page, or after five seconds have elapsed: | ||
</PageText> | ||
<CodeBlock language="typescript" content={code} /> | ||
<PageText>Right-click on the image below to show the context menu:</PageText> | ||
<ImperativeHidingExample /> | ||
<PageNavigation of={PATH} /> | ||
</Container> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.