pre-installations and preparations

This commit is contained in:
MuhammadKhalilzadeh committed 2024-05-05 11:21:00 +03:30
1 parent 3e5a0329cb
commit 201d621ac3
6977 files changed
+742826

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
import * as React from 'react';
import { DropdownAction, DropdownState } from './useDropdown.types';
export interface DropdownContextValue {
dispatch: React.Dispatch<DropdownAction>;
popupId: string;
registerPopup: (popupId: string) => void;
registerTrigger: (element: HTMLElement | null) => void;
state: DropdownState;
triggerElement: HTMLElement | null;
}
declare const DropdownContext: React.Context<DropdownContextValue | null>;
export { DropdownContext };
+6
View File
@@ -0,0 +1,6 @@
import * as React from 'react';
const DropdownContext = /*#__PURE__*/React.createContext(null);
if (process.env.NODE_ENV !== 'production') {
DropdownContext.displayName = 'DropdownContext';
}
export { DropdownContext };
+2
View File
@@ -0,0 +1,2 @@
import { DropdownAction, DropdownState } from './useDropdown.types';
export declare function dropdownReducer(state: DropdownState, action: DropdownAction): DropdownState;
+32
View File
@@ -0,0 +1,32 @@
import { DropdownActionTypes } from './useDropdown.types';
export function dropdownReducer(state, action) {
switch (action.type) {
case DropdownActionTypes.blur:
return {
open: false,
changeReason: action.event
};
case DropdownActionTypes.escapeKeyDown:
return {
open: false,
changeReason: action.event
};
case DropdownActionTypes.toggle:
return {
open: !state.open,
changeReason: action.event
};
case DropdownActionTypes.open:
return {
open: true,
changeReason: action.event
};
case DropdownActionTypes.close:
return {
open: false,
changeReason: action.event
};
default:
throw new Error(`Unhandled action`);
}
}
+3
View File
@@ -0,0 +1,3 @@
export * from './useDropdown';
export * from './useDropdown.types';
export * from './DropdownContext';
+5
View File
@@ -0,0 +1,5 @@
'use client';
export * from './useDropdown';
export * from './useDropdown.types';
export * from './DropdownContext';
+6
View File
@@ -0,0 +1,6 @@
{
"sideEffects": false,
"module": "./index.js",
"main": "../node/useDropdown/index.js",
"types": "./index.d.ts"
}
+16
View File
@@ -0,0 +1,16 @@
import type { DropdownContextValue } from './DropdownContext';
import { UseDropdownParameters } from './useDropdown.types';
/**
*
* Demos:
*
* - [Menu](https://mui.com/base-ui/react-menu/#hooks)
*
* API:
*
* - [useDropdown API](https://mui.com/base-ui/react-menu/hooks-api/#use-dropdown)
*/
export declare function useDropdown(parameters?: UseDropdownParameters): {
contextValue: DropdownContextValue;
open: boolean;
};
+67
View File
@@ -0,0 +1,67 @@
'use client';
import * as React from 'react';
import { useControllableReducer } from '../utils/useControllableReducer';
import { DropdownActionTypes } from './useDropdown.types';
import { dropdownReducer } from './dropdownReducer';
/**
*
* Demos:
*
* - [Menu](https://mui.com/base-ui/react-menu/#hooks)
*
* API:
*
* - [useDropdown API](https://mui.com/base-ui/react-menu/hooks-api/#use-dropdown)
*/
export function useDropdown(parameters = {}) {
const {
defaultOpen,
onOpenChange,
open: openProp,
componentName = 'useDropdown'
} = parameters;
const [popupId, setPopupId] = React.useState('');
const [triggerElement, setTriggerElement] = React.useState(null);
const lastActionType = React.useRef(null);
const handleStateChange = React.useCallback((event, field, value, reason) => {
if (field === 'open') {
onOpenChange == null || onOpenChange(event, value);
}
lastActionType.current = reason;
}, [onOpenChange]);
const controlledProps = React.useMemo(() => openProp !== undefined ? {
open: openProp
} : {}, [openProp]);
const [state, dispatch] = useControllableReducer({
controlledProps,
initialState: defaultOpen ? {
open: true,
changeReason: null
} : {
open: false,
changeReason: null
},
onStateChange: handleStateChange,
reducer: dropdownReducer,
componentName
});
React.useEffect(() => {
if (!state.open && lastActionType.current !== null && lastActionType.current !== DropdownActionTypes.blur) {
triggerElement == null || triggerElement.focus();
}
}, [state.open, triggerElement]);
const contextValue = {
state,
dispatch,
popupId,
registerPopup: setPopupId,
registerTrigger: setTriggerElement,
triggerElement
};
return {
contextValue,
open: state.open
};
}
+66
View File
@@ -0,0 +1,66 @@
/// <reference types="react" />
import type { DropdownContextValue } from './DropdownContext';
export interface UseDropdownParameters {
/**
* If `true`, the dropdown is initially open.
*/
defaultOpen?: boolean;
/**
* Callback fired when the component requests to be opened or closed.
*/
onOpenChange?: (event: React.MouseEvent | React.KeyboardEvent | React.FocusEvent | null, open: boolean) => void;
/**
* Allows to control whether the dropdown is open.
* This is a controlled counterpart of `defaultOpen`.
*/
open?: boolean;
/**
* The name of the component using useDropdown.
* For debugging purposes.
* @default 'useDropdown'
*/
componentName?: string;
}
export interface UseDropdownReturnValue {
/**
* The value to be passed into the DropdownContext provider.
*/
contextValue: DropdownContextValue;
/**
* If `true`, the dropdown is open.
*/
open: boolean;
}
export declare const DropdownActionTypes: {
readonly blur: "dropdown:blur";
readonly escapeKeyDown: "dropdown:escapeKeyDown";
readonly toggle: "dropdown:toggle";
readonly open: "dropdown:open";
readonly close: "dropdown:close";
};
interface DropdownBlurAction {
type: typeof DropdownActionTypes.blur;
event: React.FocusEvent;
}
interface DropdownEscapeKeyDownAction {
type: typeof DropdownActionTypes.escapeKeyDown;
event: React.KeyboardEvent;
}
interface DropdownToggleAction {
type: typeof DropdownActionTypes.toggle;
event: React.MouseEvent | React.KeyboardEvent | React.FocusEvent | null;
}
interface DropdownOpenAction {
type: typeof DropdownActionTypes.open;
event: React.MouseEvent | React.KeyboardEvent | React.FocusEvent | null;
}
interface DropdownCloseAction {
type: typeof DropdownActionTypes.close;
event: React.MouseEvent | React.KeyboardEvent | React.FocusEvent | null;
}
export type DropdownAction = DropdownBlurAction | DropdownEscapeKeyDownAction | DropdownToggleAction | DropdownOpenAction | DropdownCloseAction;
export type DropdownState = {
open: boolean;
changeReason: React.MouseEvent | React.KeyboardEvent | React.FocusEvent | null;
};
export {};
+7
View File
@@ -0,0 +1,7 @@
export const DropdownActionTypes = {
blur: 'dropdown:blur',
escapeKeyDown: 'dropdown:escapeKeyDown',
toggle: 'dropdown:toggle',
open: 'dropdown:open',
close: 'dropdown:close'
};