pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+2
@@ -0,0 +1,2 @@
|
||||
export { useTab } from './useTab';
|
||||
export * from './useTab.types';
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
'use client';
|
||||
|
||||
export { useTab } from './useTab';
|
||||
export * from './useTab.types';
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"sideEffects": false,
|
||||
"module": "./index.js",
|
||||
"main": "../node/useTab/index.js",
|
||||
"types": "./index.d.ts"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import { UseTabParameters, UseTabReturnValue } from './useTab.types';
|
||||
/**
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Tabs](https://mui.com/base-ui/react-tabs/#hooks)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useTab API](https://mui.com/base-ui/react-tabs/hooks-api/#use-tab)
|
||||
*/
|
||||
declare function useTab(parameters: UseTabParameters): UseTabReturnValue;
|
||||
export { useTab };
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
'use client';
|
||||
|
||||
import _extends from "@babel/runtime/helpers/esm/extends";
|
||||
import * as React from 'react';
|
||||
import { unstable_useId as useId, unstable_useForkRef as useForkRef } from '@mui/utils';
|
||||
import { useTabsContext } from '../Tabs';
|
||||
import { extractEventHandlers } from '../utils/extractEventHandlers';
|
||||
import { useCompoundItem } from '../useCompound';
|
||||
import { useListItem } from '../useList';
|
||||
import { useButton } from '../useButton';
|
||||
import { combineHooksSlotProps } from '../utils/combineHooksSlotProps';
|
||||
function tabValueGenerator(otherTabValues) {
|
||||
return otherTabValues.size;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Tabs](https://mui.com/base-ui/react-tabs/#hooks)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useTab API](https://mui.com/base-ui/react-tabs/hooks-api/#use-tab)
|
||||
*/
|
||||
function useTab(parameters) {
|
||||
const {
|
||||
value: valueParam,
|
||||
rootRef: externalRef,
|
||||
disabled = false,
|
||||
id: idParam
|
||||
} = parameters;
|
||||
const tabRef = React.useRef(null);
|
||||
const id = useId(idParam);
|
||||
const {
|
||||
value: selectedValue,
|
||||
selectionFollowsFocus,
|
||||
getTabPanelId
|
||||
} = useTabsContext();
|
||||
const tabMetadata = React.useMemo(() => ({
|
||||
disabled,
|
||||
ref: tabRef,
|
||||
id
|
||||
}), [disabled, tabRef, id]);
|
||||
const {
|
||||
id: value,
|
||||
index,
|
||||
totalItemCount: totalTabsCount
|
||||
} = useCompoundItem(valueParam != null ? valueParam : tabValueGenerator, tabMetadata);
|
||||
const {
|
||||
getRootProps: getTabProps,
|
||||
highlighted,
|
||||
selected
|
||||
} = useListItem({
|
||||
item: value
|
||||
});
|
||||
const {
|
||||
getRootProps: getButtonProps,
|
||||
rootRef: buttonRefHandler,
|
||||
active,
|
||||
focusVisible,
|
||||
setFocusVisible
|
||||
} = useButton({
|
||||
disabled,
|
||||
focusableWhenDisabled: !selectionFollowsFocus,
|
||||
type: 'button'
|
||||
});
|
||||
const handleRef = useForkRef(tabRef, externalRef, buttonRefHandler);
|
||||
const tabPanelId = value !== undefined ? getTabPanelId(value) : undefined;
|
||||
const getRootProps = (externalProps = {}) => {
|
||||
const externalEventHandlers = extractEventHandlers(externalProps);
|
||||
const getCombinedRootProps = combineHooksSlotProps(getTabProps, getButtonProps);
|
||||
return _extends({}, externalProps, getCombinedRootProps(externalEventHandlers), {
|
||||
role: 'tab',
|
||||
'aria-controls': tabPanelId,
|
||||
'aria-selected': selected,
|
||||
id,
|
||||
ref: handleRef
|
||||
});
|
||||
};
|
||||
return {
|
||||
getRootProps,
|
||||
active,
|
||||
focusVisible,
|
||||
highlighted,
|
||||
index,
|
||||
rootRef: handleRef,
|
||||
// the `selected` state isn't set on the server (it relies on effects to be calculated),
|
||||
// so we fall back to checking the `value` prop with the selectedValue from the TabsContext
|
||||
selected: selected || value === selectedValue,
|
||||
setFocusVisible,
|
||||
totalTabsCount
|
||||
};
|
||||
}
|
||||
export { useTab };
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
import * as React from 'react';
|
||||
import { UseButtonRootSlotProps } from '../useButton';
|
||||
export interface UseTabParameters {
|
||||
/**
|
||||
* The value of the tab.
|
||||
* It's used to associate the tab with a tab panel(s) with the same value.
|
||||
* If the value is not provided, it falls back to the position index.
|
||||
*/
|
||||
value?: number | string;
|
||||
/**
|
||||
* If `true`, the tab will be disabled.
|
||||
*/
|
||||
onChange?: (event: React.SyntheticEvent, value: number | string) => void;
|
||||
/**
|
||||
* Callback fired when the tab is clicked.
|
||||
*/
|
||||
onClick?: React.MouseEventHandler;
|
||||
/**
|
||||
* If `true`, the tab will be disabled.
|
||||
*/
|
||||
disabled?: boolean;
|
||||
/**
|
||||
* The id of the tab.
|
||||
* If not provided, it will be automatically generated.
|
||||
*/
|
||||
id?: string;
|
||||
/**
|
||||
* Ref to the root slot's DOM element.
|
||||
*/
|
||||
rootRef?: React.Ref<Element>;
|
||||
}
|
||||
export type UseTabRootSlotProps<ExternalProps = {}> = UseButtonRootSlotProps<ExternalProps> & {
|
||||
'aria-controls': React.AriaAttributes['aria-controls'];
|
||||
'aria-selected': React.AriaAttributes['aria-selected'];
|
||||
id: string | undefined;
|
||||
ref: React.RefCallback<Element> | null;
|
||||
role: React.AriaRole;
|
||||
};
|
||||
export interface UseTabReturnValue {
|
||||
/**
|
||||
* Resolver for the root slot's props.
|
||||
* @param externalProps props for the root slot
|
||||
* @returns props that should be spread on the root slot
|
||||
*/
|
||||
getRootProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseTabRootSlotProps<ExternalProps>;
|
||||
/**
|
||||
* If `true`, the tab is active (as in `:active` pseudo-class; in other words, pressed).
|
||||
*/
|
||||
active: boolean;
|
||||
/**
|
||||
* If `true`, the tab has visible focus.
|
||||
* This is a workaround for browsers that do not support this feature natively.
|
||||
*/
|
||||
focusVisible: boolean;
|
||||
/**
|
||||
* If `true`, the tab is highlighted.
|
||||
*/
|
||||
highlighted: boolean;
|
||||
/**
|
||||
* 0-based index of the tab in the list of tabs.
|
||||
*/
|
||||
index: number;
|
||||
/**
|
||||
* Ref to the root slot's DOM element.
|
||||
*/
|
||||
rootRef: React.RefCallback<Element> | null;
|
||||
/**
|
||||
* If `true`, the tab is selected.
|
||||
*/
|
||||
selected: boolean;
|
||||
/**
|
||||
* Sets the focus-visible state of the tab.
|
||||
* This is a workaround for browsers that do not support this feature natively.
|
||||
*/
|
||||
setFocusVisible: React.Dispatch<React.SetStateAction<boolean>>;
|
||||
/**
|
||||
* Total number of tabs in the nearest parent TabsList.
|
||||
* This can be used to determine if the tab is the last one to style it accordingly.
|
||||
*/
|
||||
totalTabsCount: number;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
Reference in new issue
Block a user