Skip to content

Commit 1e0e783

Browse files
committed
chore(docs): Added a new Hover Mode demo
1 parent c5a08da commit 1e0e783

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
`react-md@2.8.0` introduces a new public hover mode API that allows different
2+
temporary elements like tooltips and popover dialogs to appear immediately after
3+
an element has been hovered instead of waiting the default hover duration
4+
(`1s`). To use this functionality you'll need to:
5+
6+
- render the `HoverModeProvider` or `Configuration` (from #layout) component as
7+
a parent component
8+
- use the `useHoverMode` hook to provide the mouse event handlers, visibility,
9+
and other functionality
10+
11+
I recommend checking out the
12+
[useHoverMode type definitions]({{GITHUB_FILE_URL}}/packages/utils/src/hover/useHoverMode.ts)
13+
for more information around what's returned.
14+
15+
The example below will show how you can use the hover mode API to create a
16+
Wikipedia-like preview window while hovering over links.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React, { ReactElement } from "react";
2+
import { Text } from "@react-md/typography";
3+
import { HoverModeProvider } from "@react-md/utils";
4+
5+
import Markdown from "components/Markdown/Markdown";
6+
import WikipediaPreviewLink from "components/WikipediaPreviewLink";
7+
8+
function DesignLanguageDescription(): ReactElement {
9+
return (
10+
<Markdown>
11+
A **design language** or **design vocabulary** is an overarching scheme or
12+
style that guides the design of a complement of products or architectural
13+
settings.
14+
</Markdown>
15+
);
16+
}
17+
18+
function GoogleNowDescription(): ReactElement {
19+
return (
20+
<Markdown>
21+
**Google Now** was a feature of Google Search of the Google app for
22+
Android and iOS. Google Now proactively delivered information to users to
23+
predict (based on search habits and other factors) information they may
24+
need in the form of informational cards. Google Now branding is no longer
25+
used, but the functionality continues in the Google app and its feed.
26+
</Markdown>
27+
);
28+
}
29+
30+
export default function HoverMode(): ReactElement {
31+
return (
32+
// Note: this can be ignored if you are using the `Configuration` component
33+
// from `@react-md/utils`
34+
<HoverModeProvider>
35+
<Text>
36+
{/* copy/pasted from https://en.wikipedia.org/wiki/Material_Design */}
37+
<strong>Material Design</strong> (codenamed{" "}
38+
<strong>Quantum Paper</strong>){" "}
39+
<WikipediaPreviewLink
40+
href="https://en.wikipedia.org/wiki/Design_language"
41+
preview={<DesignLanguageDescription />}
42+
>
43+
Design Language
44+
</WikipediaPreviewLink>{" "}
45+
{'"card" motifs that debuted in '}
46+
<WikipediaPreviewLink
47+
href="https://en.wikipedia.org/wiki/Google_Now"
48+
preview={<GoogleNowDescription />}
49+
>
50+
Google Now
51+
</WikipediaPreviewLink>{" "}
52+
, Material Design uses more grid-based layouts, responsive animations
53+
and transitions, padding, and depth effects such as lighting and
54+
shadows. Google announced Material Design on June 25, 2014, at the 2014
55+
Google I/O conference.
56+
</Text>
57+
</HoverModeProvider>
58+
);
59+
}

packages/documentation/src/components/Demos/Utils/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import simpleGridList from "./SimpleGridList.md";
2323
import GridListSize from "./GridListSize";
2424
import gridListSize from "./GridListSize.md";
2525

26+
import HoverMode from "./HoverMode";
27+
import hoverMode from "./HoverMode.md";
28+
2629
const demos = [
2730
{
2831
name: "App Size Listener Example",
@@ -59,6 +62,11 @@ const demos = [
5962
description: gridListSize,
6063
children: <GridListSize />,
6164
},
65+
{
66+
name: "Hover Mode",
67+
description: hoverMode,
68+
children: <HoverMode />,
69+
},
6270
];
6371

6472
export default function Utils(): ReactElement {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.dialog {
2+
cursor: pointer;
3+
max-height: 10rem;
4+
max-width: 20rem;
5+
overflow: hidden;
6+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React, { ReactElement, ReactNode, useRef } from "react";
2+
import { DialogContent, FixedDialog } from "@react-md/dialog";
3+
import { Link } from "@react-md/link";
4+
import { BELOW_CENTER_ANCHOR, useHoverMode } from "@react-md/utils";
5+
6+
import { useId } from "components/IdProvider";
7+
8+
import styles from "./WikipediaPreviewLink.module.scss";
9+
10+
export interface WikipediaPreviewLinkProps {
11+
/**
12+
* The wikipedia URL
13+
*/
14+
href: string;
15+
16+
/**
17+
* The content to display in the preview window.
18+
*/
19+
preview: ReactNode;
20+
21+
/**
22+
* The link contents.
23+
*/
24+
children: ReactNode;
25+
}
26+
27+
export default function WikipediaPreviewLink({
28+
href,
29+
preview,
30+
children,
31+
}: WikipediaPreviewLinkProps): ReactElement {
32+
const id = useId("wiki");
33+
const { active, handlers, visible, setVisible } = useHoverMode();
34+
35+
const linkRef = useRef<HTMLAnchorElement>(null);
36+
return (
37+
<>
38+
<Link ref={linkRef} href={href} {...handlers}>
39+
{children}
40+
</Link>
41+
<FixedDialog
42+
aria-label="Wikipedia Preview"
43+
id={id}
44+
visible={visible}
45+
onRequestClose={() => setVisible(false)}
46+
{...handlers}
47+
fixedTo={linkRef.current}
48+
onClick={() => {
49+
window.location.href = href;
50+
}}
51+
disableFocusContainer={active}
52+
disableScrollLock={!active}
53+
anchor={BELOW_CENTER_ANCHOR}
54+
options={{ preventOverlap: true }}
55+
overlay={active ? false : undefined}
56+
className={styles.dialog}
57+
>
58+
<DialogContent>{preview}</DialogContent>
59+
</FixedDialog>
60+
</>
61+
);
62+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "./WikipediaPreviewLink";

0 commit comments

Comments
 (0)