Skip to content

Commit ae7e43a

Browse files
committed
Make some types generic, rename id to menuId in hooks
1 parent b85f5e2 commit ae7e43a

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

lib/handlers.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ export declare enum DataAttributes {
22
MenuId = "data-contextmenu-menu-id",
33
DataId = "data-contextmenu-data-id"
44
}
5-
export interface ContextMenuEvent {
5+
export interface ContextMenuEvent<DataType = any> {
66
clientX: number;
77
clientY: number;
8-
data: any;
8+
data: DataType;
99
}
1010
export declare const GlobalHandlers: {
1111
readonly handleContextMenu: (e: MouseEvent) => void;

lib/hooks.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import React from 'react';
22
import { Nullable } from 'tsdef';
33
import { ContextMenuEvent } from './handlers';
44
export declare const useLazyValue: <T>(factory: () => T) => T;
5-
export declare const ContextMenuEventContext: React.Context<Nullable<ContextMenuEvent>>;
6-
export declare const useContextMenuEvent: () => Nullable<ContextMenuEvent>;
5+
export declare const ContextMenuEventContext: React.Context<Nullable<ContextMenuEvent<any>>>;
6+
export declare const useContextMenuEvent: <DataType = any>() => Nullable<ContextMenuEvent<DataType>>;
77
export declare const useContextMenuTrigger: <RefType extends HTMLElement>(parameters: {
8-
id?: string | undefined;
8+
menuId?: string | undefined;
99
data?: any;
1010
ref?: React.RefObject<RefType> | undefined;
1111
}) => React.RefObject<RefType>;
12-
export declare const useMenuToggleMethods: (lastShowMenuEvent: Nullable<ContextMenuEvent>, setShowMenuEvent: (event: Nullable<ContextMenuEvent>) => void, onShow?: ((event: ContextMenuEvent) => void) | undefined, onHide?: (() => void) | undefined) => [(event: ContextMenuEvent) => void, () => void];
13-
export declare const useMenuPlacementStyle: (wrapperRef: React.RefObject<HTMLElement>, lastShowMenuEvent: Nullable<ContextMenuEvent>, setMenuPlacementStyle: (style: Nullable<React.CSSProperties>) => void) => void;
14-
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;
12+
export declare const useMenuToggleMethods: (lastShowMenuEvent: Nullable<ContextMenuEvent<any>>, setShowMenuEvent: (event: Nullable<ContextMenuEvent<any>>) => void, onShow?: ((event: ContextMenuEvent<any>) => void) | undefined, onHide?: (() => void) | undefined) => [(event: ContextMenuEvent<any>) => void, () => void];
13+
export declare const useMenuPlacementStyle: (wrapperRef: React.RefObject<HTMLElement>, lastShowMenuEvent: Nullable<ContextMenuEvent<any>>, setMenuPlacementStyle: (style: Nullable<React.CSSProperties>) => void) => void;
14+
export declare const useInternalHandlers: (wrapperRef: React.RefObject<HTMLElement>, lastShowMenuEvent: Nullable<ContextMenuEvent<any>>, showMenu: (event: ContextMenuEvent<any>) => 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: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/hooks.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.

src/handlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ export enum DataAttributes {
1616
DataId = 'data-contextmenu-data-id',
1717
}
1818

19-
export interface ContextMenuEvent {
19+
export interface ContextMenuEvent<DataType = any> {
2020
clientX: number;
2121
clientY: number;
22-
data: any;
22+
data: DataType; // user-defined data
2323
}
2424

2525
type ContextMenuTarget = EventTarget & Element;

src/hooks.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ export const useLazyValue = <T>(factory: () => T): T => {
2424
};
2525

2626
export const ContextMenuEventContext = React.createContext<Nullable<ContextMenuEvent>>(null);
27-
export const useContextMenuEvent = (): Nullable<ContextMenuEvent> => {
27+
export const useContextMenuEvent = <DataType = any>(): Nullable<ContextMenuEvent<DataType>> => {
2828
return useContext(ContextMenuEventContext);
2929
};
3030

3131
export const useContextMenuTrigger = <RefType extends HTMLElement>(
32-
parameters: { id?: string; data?: any, ref?: React.RefObject<RefType> },
32+
parameters: { menuId?: string; data?: any, ref?: React.RefObject<RefType> },
3333
): React.RefObject<RefType> => {
34-
const {id, data} = parameters;
34+
const {menuId, data} = parameters;
3535

3636
// If user did not provide a ref, we simply generate a new one.
3737
const ref = parameters.ref ? parameters.ref : useRef<RefType>(null);
@@ -48,7 +48,7 @@ export const useContextMenuTrigger = <RefType extends HTMLElement>(
4848
const {current} = ref;
4949
if (!current) return;
5050

51-
if (id) current.setAttribute(DataAttributes.MenuId, id);
51+
if (menuId) current.setAttribute(DataAttributes.MenuId, menuId);
5252
current.setAttribute(DataAttributes.DataId, dataId);
5353
current.addEventListener('contextmenu', LocalHandlers.handleContextMenu);
5454
current.addEventListener('touchstart', LocalHandlers.handleTouchStart);

0 commit comments

Comments
 (0)