Skip to content

Commit 0043bb1

Browse files
committed
Extend internal hooks to handle all dismissal interactions
1 parent 39e5e97 commit 0043bb1

16 files changed

+134
-37
lines changed

docs/components/BasicGlobalMenu.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1-
import React, { useCallback, useState } from 'react';
1+
import React, { useCallback, useEffect, useState } from 'react';
22
import { ContextMenuWrapper } from 'react-context-menu-wrapper';
33

4+
const MyContextMenu = () => {
5+
const [seconds, setSeconds] = useState(0);
6+
useEffect(() => {
7+
const timer = setInterval(() => setSeconds(s => s + 1), 1000);
8+
return () => clearInterval(timer);
9+
});
10+
11+
return (
12+
<div className="context-menu">
13+
<p>
14+
I am a custom <strong>global</strong> context menu!
15+
</p>
16+
<p>
17+
Open for {seconds} {seconds === 1 ? 'second' : 'seconds'}.
18+
</p>
19+
</div>
20+
);
21+
};
22+
423
const GlobalMenuExample = () => {
524
const [globalMenuEnabled, setGlobalMenuEnabled] = useState(true);
625
const onClick = useCallback(() => setGlobalMenuEnabled(!globalMenuEnabled), [globalMenuEnabled]);
@@ -17,9 +36,7 @@ const GlobalMenuExample = () => {
1736

1837
{globalMenuEnabled && (
1938
<ContextMenuWrapper global={true}>
20-
<div className="context-menu">
21-
<p>I am a global context menu!</p>
22-
</div>
39+
<MyContextMenu />
2340
</ContextMenuWrapper>
2441
)}
2542
</React.Fragment>

docs/components/BasicLocalMenu.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ const LocalMenuExample = () => {
1515
</div>
1616

1717
<ContextMenuWrapper id={menuId}>
18-
<div className="context-menu">
19-
<p>I am component-local context menu!</p>
18+
<div className="context-menu" style={{ backgroundColor: '#d6fffc' }}>
19+
<p>
20+
I am <strong>component-local</strong> context menu!
21+
</p>
2022
</div>
2123
</ContextMenuWrapper>
2224
</React.Fragment>

docs/components/SharedMenu.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import React, { useCallback, useRef, useState } from 'react';
22
import { ContextMenuWrapper, useContextMenuEvent, useContextMenuHandlers } from 'react-context-menu-wrapper';
33

4-
const MyContextMenu = ({ toggleSelection }) => {
4+
const MyContextMenu = ({ selection, toggleSelection }) => {
55
const menuEvent = useContextMenuEvent();
66
if (!menuEvent) return null;
77

88
const name = menuEvent.data;
99
const onClick = useCallback(() => (name ? toggleSelection(name) : null), [name, toggleSelection]);
10+
const style = { backgroundColor: selection[name] ? '#d6fffc' : '#ffe4e4' };
1011
return (
11-
<div className="context-menu">
12+
<div className="context-menu" style={style}>
1213
<p>You chose {name ? name : 'no one'}.</p>
1314
<button onClick={onClick}>Toggle state</button>
1415
</div>
@@ -48,8 +49,8 @@ const SharedMenu = () => {
4849
<Box key={name} name={name} selected={selection[name]} menuId={menuId} />
4950
))}
5051
</div>
51-
<ContextMenuWrapper id={menuId}>
52-
<MyContextMenu toggleSelection={toggleSelection} />
52+
<ContextMenuWrapper id={menuId} hideOnSelfClick={false}>
53+
<MyContextMenu selection={selection} toggleSelection={toggleSelection} />
5354
</ContextMenuWrapper>
5455
</div>
5556
);

docs/markdown/2-Basic-menus.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const LocalMenuExample = () => {
4545

4646
return (
4747
<React.Fragment>
48-
// We attach `bannerRef` to the component that will trigger the context menu:
48+
{/* We attach `bannerRef` to the component that will trigger the context menu: */}
4949
<div ref={bannerRef} className="banner">Right click or long-press this box</div>
5050

5151
<ContextMenuWrapper id={menuId}>

lib/ContextMenuWrapper.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ContextMenuWrapper.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/hooks.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ export declare const useContextMenuHandlers: (ref: React.RefObject<HTMLElement>,
99
data?: any;
1010
}) => void;
1111
export declare const useMenuToggleMethods: (lastShowMenuEvent: Nullable<ContextMenuEvent>, setShowMenuEvent: (event: Nullable<ContextMenuEvent>) => void, onShow?: ((event: ContextMenuEvent) => void) | undefined, onHide?: (() => void) | undefined) => [(event: ContextMenuEvent) => void, () => void];
12-
export declare const useMenuPlacementStyle: (lastShowMenuEvent: Nullable<ContextMenuEvent>, setMenuPlacementStyle: (style: Nullable<React.CSSProperties>) => void, wrapperRef: React.RefObject<HTMLElement>) => void;
13-
export declare const useInternalHandlers: (showMenu: (event: ContextMenuEvent) => void, hideMenu: () => void, id?: string | undefined, global?: boolean | undefined, hideOnSelfClick?: boolean | undefined, hideOnOutsideClick?: boolean | undefined, hideOnEscape?: boolean | undefined, hideOnScroll?: boolean | undefined, hideOnWindowResize?: boolean | undefined) => void;
12+
export declare const useMenuPlacementStyle: (wrapperRef: React.RefObject<HTMLElement>, lastShowMenuEvent: Nullable<ContextMenuEvent>, setMenuPlacementStyle: (style: Nullable<React.CSSProperties>) => void) => void;
13+
export declare const useInternalHandlers: (wrapperRef: React.RefObject<HTMLElement>, lastShowMenuEvent: Nullable<ContextMenuEvent>, showMenu: (event: ContextMenuEvent) => void, hideMenu: () => void, id?: string | undefined, global?: boolean | undefined, hideOnSelfClick?: boolean | undefined, hideOnOutsideClick?: boolean | undefined, hideOnEscape?: boolean | undefined, hideOnScroll?: boolean | undefined, hideOnWindowResize?: boolean | undefined) => void;

lib/hooks.js

Lines changed: 42 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)