-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathuse-code.ts
42 lines (33 loc) · 1.06 KB
/
use-code.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
import type {CodeVariantProps} from "@nextui-org/theme";
import type {HTMLNextUIProps, PropGetter} from "@nextui-org/system-rsc";
import {code} from "@nextui-org/theme";
import {mapPropsVariants} from "@nextui-org/system-rsc";
import {ReactRef} from "@nextui-org/react-utils";
import {useMemo} from "react";
export interface UseCodeProps extends HTMLNextUIProps<"code">, CodeVariantProps {
/**
* Ref to the DOM node.
*/
ref?: ReactRef<HTMLElement | null>;
}
export function useCode(originalProps: UseCodeProps) {
const [props, variantProps] = mapPropsVariants(originalProps, code.variantKeys);
const {as, children, className, ...otherProps} = props;
const Component = as || "code";
const classNames = useMemo(
() =>
code({
...variantProps,
className,
}),
[...Object.values(variantProps), className],
);
const getCodeProps: PropGetter = () => {
return {
className: classNames,
...otherProps,
};
};
return {Component, children, getCodeProps};
}
export type UseCodeReturn = ReturnType<typeof useCode>;