-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
use-kbd.ts
67 lines (54 loc) 路 1.88 KB
/
use-kbd.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import type {KbdVariantProps, KbdSlots, SlotsToClasses} from "@nextui-org/theme";
import type {HTMLNextUIProps, PropGetter} from "@nextui-org/system-rsc";
import {mapPropsVariants} from "@nextui-org/system-rsc";
import {kbd} from "@nextui-org/theme";
import {clsx} from "@nextui-org/shared-utils";
import {ReactRef} from "@nextui-org/react-utils";
import {useMemo} from "react";
import {KbdKey} from "./utils";
interface Props extends HTMLNextUIProps<"kbd"> {
/**
* Ref to the DOM node.
*/
ref?: ReactRef<HTMLElement | null>;
/**
* The key or keys to be displayed.
*/
keys?: KbdKey | KbdKey[];
/**
* Classname or List of classes to change the classNames of the element.
* if `className` is passed, it will be added to the base slot.
*
* @example
* ```ts
* <Kbd classNames={{
* base:"base-classes",
* abbr: "abbr-classes", // the key wrapper
* content: "content-classes", // the children wrapper
* }} />
* ```
*/
classNames?: SlotsToClasses<KbdSlots>;
}
export type UseKbdProps = Props & KbdVariantProps;
export function useKbd(originalProps: UseKbdProps) {
const [props, variantProps] = mapPropsVariants(originalProps, kbd.variantKeys);
const {as, children, className, keys, title, classNames, ...otherProps} = props;
const Component = as || "kbd";
const slots = useMemo(
() =>
kbd({
...variantProps,
}),
[...Object.values(variantProps)],
);
const baseStyles = clsx(classNames?.base, className);
const keysToRender = typeof keys === "string" ? [keys] : Array.isArray(keys) ? keys : [];
const getKbdProps: PropGetter = (props = {}) => ({
...otherProps,
...props,
className: clsx(slots.base({class: clsx(baseStyles, props.className)})),
});
return {Component, slots, classNames, title, children, keysToRender, getKbdProps};
}
export type UseKbdReturn = ReturnType<typeof useKbd>;