mm
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
type CSSProperties,
|
||||
} from "react";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { displayRecipe } from "../../styles/recipes/display-recipe";
|
||||
import { dotSizeRecipe } from "../../styles/recipes/dot-size-recipe";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { brandRecipe } from "../../styles/recipes/brand-recipe";
|
||||
|
||||
export type BadgeProps = CommonProps & {
|
||||
size?: "xs" | "sm" | "md" | "lg";
|
||||
} & {
|
||||
isDot?: boolean;
|
||||
indicatorContent?: string;
|
||||
};
|
||||
|
||||
export const Badge = (props: BadgeProps) => {
|
||||
const {
|
||||
indicatorContent = "",
|
||||
isDot = false,
|
||||
size = "md",
|
||||
children,
|
||||
className = "",
|
||||
ref,
|
||||
...rest
|
||||
} = props;
|
||||
|
||||
const wrapperStyle: CSSProperties = {
|
||||
position: "relative",
|
||||
height: "fit",
|
||||
width: "fit",
|
||||
};
|
||||
|
||||
const indicatorStyle: CSSProperties = {
|
||||
position: "absolute",
|
||||
top: 0,
|
||||
right: 0,
|
||||
transform: "translate(50%,-50%)",
|
||||
borderRadius: "9999px",
|
||||
};
|
||||
|
||||
|
||||
|
||||
const indicatorClass = mergeClass(displayRecipe(), dotSizeRecipe({ size }), brandRecipe({ brand: "danger" }), "pointer-events--none", className);
|
||||
|
||||
return (
|
||||
<div {...rest} ref={ref} style={wrapperStyle}>
|
||||
{children}
|
||||
<div className={indicatorClass} style={indicatorStyle}>
|
||||
{indicatorContent}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,115 @@
|
||||
import React, { useState, type CSSProperties, type ReactNode } from "react";
|
||||
import { SpinnerSvg } from "../../assets/svg/SpinnerSvg";
|
||||
import { Icon } from "../icon/icon";
|
||||
import { displayRecipe } from "../../styles/recipes/display-recipe";
|
||||
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
|
||||
import { variantRecipe } from "../../styles/recipes/variant-recipe";
|
||||
import { brandRecipe } from "../../styles/recipes/brand-recipe";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
|
||||
export type ButtonProps = CommonProps & {
|
||||
size?: "xs" | "sm" | "md" | "lg";
|
||||
variant?: "filled" | "outline" | "subtle" | "ghost";
|
||||
shape?: "rounded" | "square" | "circle";
|
||||
brand?: "success" | "danger" | "info" | "warning" | "default";
|
||||
width?: string;
|
||||
} & {
|
||||
isIconOnly?: boolean;
|
||||
isHideIcon?: boolean;
|
||||
} & {
|
||||
canLoading?: boolean;
|
||||
isLoading?: boolean;
|
||||
} & {
|
||||
iconSvg?: ReactNode;
|
||||
loadingIconSvg?: ReactNode;
|
||||
onClick?: (e?: React.MouseEvent<Element, MouseEvent>) => void;
|
||||
};
|
||||
|
||||
export const Button = (props: ButtonProps) => {
|
||||
const {
|
||||
size = "md",
|
||||
variant = "filled",
|
||||
shape = "rounded",
|
||||
brand,
|
||||
// ===========================
|
||||
style,
|
||||
width,
|
||||
// ===========================
|
||||
isIconOnly = false,
|
||||
isHideIcon = false,
|
||||
// ===========================
|
||||
canLoading = false,
|
||||
isLoading,
|
||||
// ===========================
|
||||
isDisabled = false,
|
||||
// =========================
|
||||
className = "",
|
||||
children,
|
||||
// ==========================
|
||||
iconSvg,
|
||||
loadingIconSvg = <SpinnerSvg />,
|
||||
ref,
|
||||
onClick,
|
||||
...rest
|
||||
} = props;
|
||||
|
||||
const [innerIsLoading, setInnerIsIsLoading] = useState<{
|
||||
state: boolean;
|
||||
class: string;
|
||||
}>({ state: false, class: "" });
|
||||
|
||||
const brandFinal =
|
||||
brand != undefined ? brand : variant == "filled" ? "info" : "default";
|
||||
|
||||
// ====================================================================================
|
||||
|
||||
const rootClass = mergeClass(
|
||||
displayRecipe(),
|
||||
itemSizeRecipe({ size, shape, isIconOnly, isLongPadding: true }),
|
||||
variantRecipe({ variant, isDisabled }),
|
||||
brandRecipe({ brand: brandFinal }),
|
||||
className,
|
||||
);
|
||||
|
||||
let rootStyle: CSSProperties = {
|
||||
transitionDuration: "all 5s ease-in-out",
|
||||
width: width,
|
||||
...style,
|
||||
};
|
||||
|
||||
let loadingClass = mergeClass("");
|
||||
|
||||
// =========================================================================================
|
||||
|
||||
function clickHandler(e: React.MouseEvent<Element, MouseEvent>) {
|
||||
if (canLoading && isLoading == undefined) {
|
||||
}
|
||||
onClick?.(e);
|
||||
}
|
||||
|
||||
// =========================================================================================
|
||||
|
||||
const icon = <Icon size={size}>{iconSvg}</Icon>;
|
||||
const loadingIcon = (
|
||||
<Icon size={size} className={loadingClass}>
|
||||
{loadingIconSvg}
|
||||
</Icon>
|
||||
);
|
||||
|
||||
return (
|
||||
<button
|
||||
{...rest}
|
||||
ref={ref}
|
||||
className={rootClass}
|
||||
style={rootStyle}
|
||||
onClick={clickHandler}
|
||||
>
|
||||
{canLoading && isLoading && loadingIcon}
|
||||
{!isHideIcon && iconSvg && icon}
|
||||
{!isIconOnly && children}
|
||||
</button>
|
||||
);
|
||||
};
|
||||
|
||||
Button.displayName = "Button";
|
||||
@@ -0,0 +1,36 @@
|
||||
import { useState, type ComponentPropsWithRef } from "react";
|
||||
import { CheckIndicatorSvg } from "../../assets/svg/CheckIndicatorSvg";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
|
||||
|
||||
export type CheckboxProps = CommonProps & {
|
||||
size?: "sm" | "md",
|
||||
defaultCheck?: boolean,
|
||||
onChange?: (preCheck?: boolean, nextCheck?: boolean) => void,
|
||||
controllerCheck?: boolean
|
||||
};
|
||||
|
||||
export const Checkbox = (props: CheckboxProps) => {
|
||||
const { defaultCheck = false, ref, onChange, size = "sm", className = "" } = props
|
||||
|
||||
const [isCheck, setIsCheck] = useState<boolean>(defaultCheck)
|
||||
|
||||
const rootClass = mergeClass(inlineSizeRecipe({ size, shape: "rounded", isIconOnly: true }), className)
|
||||
|
||||
const inputClass = mergeClass("sr-only")
|
||||
|
||||
const indicatorClass = mergeClass(inlineSizeRecipe({ size, shape: "rounded" }))
|
||||
|
||||
function clickHandler() {
|
||||
const preValue = isCheck
|
||||
setIsCheck(!isCheck)
|
||||
onChange?.(preValue, isCheck)
|
||||
}
|
||||
|
||||
return <div className={rootClass} onClick={clickHandler} >
|
||||
<input className={inputClass} type="checkbox" ref={ref} />
|
||||
{isCheck && <CheckIndicatorSvg className={indicatorClass} />}
|
||||
</div>
|
||||
}
|
||||
Checkbox.displayName = "Checkbox"
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
import { Slot } from "../../utils/slot";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { inlineSizeRecipe } from "../../styles/recipes/inline-size-recipe";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
|
||||
export type IconProps = CommonProps & {
|
||||
size?: "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl";
|
||||
shape?: "square" | "circle" | "rounded";
|
||||
} & {
|
||||
onClick?: (e?: React.MouseEvent) => void
|
||||
};
|
||||
|
||||
export const Icon = (props: IconProps) => {
|
||||
const { size = "md", className = "", children, shape = "square", ref, ...rest } = props;
|
||||
|
||||
const rootClass = mergeClass(inlineSizeRecipe({ size, shape, isIconOnly: "true" }), className);
|
||||
|
||||
return (
|
||||
<Slot {...rest} ref={ref} className={rootClass}>{children}</Slot>
|
||||
);
|
||||
};
|
||||
|
||||
Icon.displayName = "Icon";
|
||||
@@ -0,0 +1,86 @@
|
||||
|
||||
import { useMenuFloatingContext } from "./use-menu-floating-context";
|
||||
import { useListItem, useMergeRefs } from "@floating-ui/react";
|
||||
import { useMenuContext } from "./use-menu-context";
|
||||
import React, { useState, type CSSProperties } from "react";
|
||||
import { Icon } from "../icon/icon";
|
||||
import { Checkbox } from "../checkbox/checkbox";
|
||||
import { displayRecipe } from "../../styles/recipes/display-recipe";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
|
||||
import { variantRecipe } from "../../styles/recipes/variant-recipe";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { containerRecipe } from "../../styles/recipes/container-recipe";
|
||||
|
||||
export type MenuCheckItemProps = CommonProps & {
|
||||
iconSvg?: React.ReactNode;
|
||||
hotkey?: string;
|
||||
check?: boolean
|
||||
onChange?: (preCheck?: boolean, nextCheck?: boolean) => void
|
||||
};
|
||||
|
||||
export const MenuCheckItem = (props: MenuCheckItemProps) => {
|
||||
const { ref, children, iconSvg = <div></div>, style, hotkey, isDisabled = false, onChange, ...rest } = props;
|
||||
|
||||
const [isCheck, setIsCheck] = useState<boolean>(false)
|
||||
|
||||
// =============== Menu Context ====================================================================
|
||||
|
||||
const menuContext = useMenuContext();
|
||||
const { size = "sm" } = menuContext!
|
||||
|
||||
// =============== Menu Floating Context ====================================================================
|
||||
|
||||
const menuFloatingContext = useMenuFloatingContext();
|
||||
const { getItemProps, activeIndex, isOpen, } = menuFloatingContext!
|
||||
|
||||
const item = useListItem();
|
||||
const isActive = item.index === activeIndex;
|
||||
|
||||
// ================== Style =================================================================
|
||||
|
||||
const rootClass = mergeClass(displayRecipe({ justifyContent: "between", }), containerRecipe({ hasFocusOutline: "false" }),
|
||||
itemSizeRecipe({ size, shape: "rounded" }),
|
||||
variantRecipe({ variant: "ghost", isDisabled }))
|
||||
|
||||
const rootStyle: CSSProperties = {
|
||||
width: "100%",
|
||||
...style
|
||||
}
|
||||
|
||||
const startSectionClass = displayRecipe()
|
||||
|
||||
const endSectionClass = displayRecipe()
|
||||
|
||||
function handleOnClick() {
|
||||
const preCheck = isCheck
|
||||
setIsCheck(!isCheck)
|
||||
onChange?.(preCheck, isCheck)
|
||||
}
|
||||
|
||||
return <button {...rest} ref={useMergeRefs([ref, item.ref])}
|
||||
className={rootClass}
|
||||
role="menuitem"
|
||||
tabIndex={isActive ? 0 : -1}
|
||||
disabled={isDisabled}
|
||||
{...getItemProps({
|
||||
style: rootStyle,
|
||||
onClick: handleOnClick
|
||||
})
|
||||
}
|
||||
>
|
||||
|
||||
<Checkbox size={size} defaultCheck={isCheck} />
|
||||
|
||||
<div className={startSectionClass}>
|
||||
<Icon size={size}>{iconSvg}</Icon>
|
||||
{children}
|
||||
</div>
|
||||
|
||||
<div className={endSectionClass}>
|
||||
{hotkey && <div>{hotkey}</div>}
|
||||
<Icon size={size}><div></div></Icon>
|
||||
</div>
|
||||
</button >
|
||||
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
import type { ComponentPropsWithoutRef, CSSProperties } from "react";
|
||||
import {
|
||||
FloatingFocusManager,
|
||||
FloatingList,
|
||||
FloatingPortal,
|
||||
} from "@floating-ui/react";
|
||||
import { useMenuFloatingContext } from "./use-menu-floating-context";
|
||||
import { useMenuContext } from "./use-menu-context";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { containerRecipe } from "../../styles/recipes/container-recipe"
|
||||
import { useThemeContext } from "../theme/use-theme-context";
|
||||
|
||||
|
||||
export type MenuDropdownProps = ComponentPropsWithoutRef<"div"> & {
|
||||
modal?: boolean;
|
||||
};
|
||||
|
||||
export const MenuDropdown = (props: MenuDropdownProps) => {
|
||||
const { modal = true, className = "", children, ...rest } = props;
|
||||
|
||||
const { size } = useMenuContext()!
|
||||
const { refs, elementsRef, context, floatingStyles, getFloatingProps, isNested, isOpen } = useMenuFloatingContext()!;
|
||||
|
||||
// ==============================================================================================================
|
||||
|
||||
const { themeBaseClass, themeClass } = useThemeContext()!
|
||||
|
||||
// ===============================================================================================================
|
||||
|
||||
const rootClass = mergeClass(themeBaseClass, themeClass, containerRecipe(), className);
|
||||
|
||||
const padding = size == "sm" ? "4px" : (size == "md" ? "8px" : undefined);
|
||||
const borderRadius = size == "sm" ? "4px" : (size == "md" ? "8px" : undefined);
|
||||
|
||||
const rootStyle: CSSProperties = {
|
||||
...floatingStyles,
|
||||
borderRadius: borderRadius,
|
||||
padding: padding,
|
||||
// invisable the outline
|
||||
outline: 0,
|
||||
background: "var(--base-bg)"
|
||||
|
||||
}
|
||||
|
||||
return <FloatingList elementsRef={elementsRef}>
|
||||
{isOpen && <FloatingPortal >
|
||||
<FloatingFocusManager context={context} modal={modal} >
|
||||
<div {...rest} className={rootClass} style={rootStyle} ref={refs.setFloating} {...getFloatingProps()} >
|
||||
{children}
|
||||
</div>
|
||||
</FloatingFocusManager>
|
||||
</FloatingPortal>
|
||||
}
|
||||
</FloatingList >
|
||||
};
|
||||
@@ -0,0 +1,107 @@
|
||||
import { useMenuFloatingContext } from "./use-menu-floating-context";
|
||||
import { useListItem, useMergeRefs } from "@floating-ui/react";
|
||||
import React, { type CSSProperties } from "react";
|
||||
import { Icon } from "../icon/icon";
|
||||
import { ChevronRightSvg } from "../../assets/svg/ChevronRightSvg";
|
||||
import { useMenuTriggerContext } from "./use-menu-trigger-context";
|
||||
import { useMenuContext } from "./use-menu-context";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { displayRecipe } from "../../styles/recipes/display-recipe";
|
||||
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
|
||||
import { variantRecipe } from "../../styles/recipes/variant-recipe";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { containerRecipe } from "../../styles/recipes/container-recipe";
|
||||
|
||||
export type MenuItemProps = CommonProps & {
|
||||
onClick?: (e?: React.MouseEvent) => void;
|
||||
iconSvg?: React.ReactNode;
|
||||
indicatorSvg?: React.ReactNode;
|
||||
hotkey?: string;
|
||||
};
|
||||
|
||||
export const MenuItem = (props: MenuItemProps) => {
|
||||
const {
|
||||
ref,
|
||||
children,
|
||||
iconSvg = <div></div>,
|
||||
indicatorSvg = <ChevronRightSvg />,
|
||||
onClick,
|
||||
style,
|
||||
hotkey,
|
||||
isDisabled = false,
|
||||
...rest
|
||||
} = props;
|
||||
|
||||
// =============== Menu Context ====================================================================
|
||||
|
||||
const { size = "sm" } = useMenuContext()!;
|
||||
|
||||
// =============== Menu Floating Context ====================================================================
|
||||
|
||||
const menuFloatingContext = useMenuFloatingContext();
|
||||
const { getItemProps, activeIndex, isOpen } = menuFloatingContext!;
|
||||
|
||||
const item = useListItem();
|
||||
const isActive = item.index === activeIndex;
|
||||
|
||||
// =============== Menu Trigger Context ====================================================================
|
||||
|
||||
const menuTriggerContext = useMenuTriggerContext();
|
||||
|
||||
const isTrigger = !!menuTriggerContext;
|
||||
|
||||
// ================== Style =================================================================
|
||||
|
||||
const rootClass = mergeClass(
|
||||
displayRecipe({ justifyContent: "between" }),
|
||||
containerRecipe({ hasFocusOutline: "false" }),
|
||||
itemSizeRecipe({ size, shape: "rounded" }),
|
||||
variantRecipe({ variant: "ghost", isDisabled }),
|
||||
);
|
||||
|
||||
const rootStyle: CSSProperties = {
|
||||
width: "100%",
|
||||
...style,
|
||||
};
|
||||
|
||||
const startSectionClass = displayRecipe();
|
||||
|
||||
const endSectionClass = displayRecipe();
|
||||
|
||||
return (
|
||||
<button
|
||||
{...rest}
|
||||
ref={useMergeRefs([ref, item.ref])}
|
||||
className={rootClass}
|
||||
role="menuitem"
|
||||
tabIndex={isActive ? 0 : -1}
|
||||
disabled={isDisabled}
|
||||
{...getItemProps({
|
||||
style: rootStyle,
|
||||
onClick: (e) => {
|
||||
onClick?.(e);
|
||||
},
|
||||
})}
|
||||
>
|
||||
<div className={startSectionClass}>
|
||||
<Icon size={size}>{iconSvg}</Icon>
|
||||
{children}
|
||||
</div>
|
||||
|
||||
<div className={endSectionClass}>
|
||||
{hotkey && <div>{hotkey}</div>}
|
||||
{isTrigger && isOpen && (
|
||||
<Icon size={size} style={{ rotate: "90deg" }}>
|
||||
{indicatorSvg}
|
||||
</Icon>
|
||||
)}
|
||||
{isTrigger && !isOpen && <Icon size={size}>{indicatorSvg}</Icon>}
|
||||
{!isTrigger && (
|
||||
<Icon size={size}>
|
||||
<div></div>
|
||||
</Icon>
|
||||
)}
|
||||
</div>
|
||||
</button>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
import { useState } from "react";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { MenuRadioItemGroupContext } from "./use-menu-radio-item-group-context";
|
||||
import { useMenuContext } from "./use-menu-context";
|
||||
|
||||
export type MenuRadioItemGroupProps = CommonProps & {
|
||||
size?: "sm" | "md";
|
||||
defaultSelectName?: string
|
||||
};
|
||||
|
||||
export const MenuRadioItemGroup = (props: MenuRadioItemGroupProps) => {
|
||||
const { size = "sm", defaultSelectName = "", ...rest } = props
|
||||
|
||||
const [selectName, setSelectName] = useState<string>(defaultSelectName);
|
||||
|
||||
const { size: menuSize } = useMenuContext()!
|
||||
|
||||
const currentSize = menuSize ?? size
|
||||
|
||||
return <MenuRadioItemGroupContext value={{ size: currentSize, selectName, setSelectName }}>
|
||||
<div {...rest} ></div>
|
||||
</MenuRadioItemGroupContext>
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { useRef } from "react";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { useMenuRadioItemGroupContext } from "./use-menu-radio-item-group-context";
|
||||
|
||||
export type MenuRadioItemProps = CommonProps & {
|
||||
name: string;
|
||||
iconSvg?: React.ReactNode;
|
||||
hotkey?: string;
|
||||
onChange?: (preCheck?: boolean, nextCheck?: boolean) => void
|
||||
};
|
||||
|
||||
export const MenuRadioItem = (props: MenuRadioItemProps) => {
|
||||
const { name, children, className, hotkey, onChange, ref, iconSvg, style } = props
|
||||
|
||||
const { selectName, setSelectName } = useMenuRadioItemGroupContext()!
|
||||
const isSelect = name === selectName
|
||||
|
||||
return <div></div>
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { ComponentPropsWithoutRef } from "react";
|
||||
|
||||
import { useMenuFloatingContext } from "./use-menu-floating-context";
|
||||
import { Slot } from "../../utils/slot";
|
||||
import { MenuTriggerContext } from "./use-menu-trigger-context";
|
||||
|
||||
export type MenuTriggerProps = ComponentPropsWithoutRef<"div">;
|
||||
|
||||
export const MenuTrigger = (props: MenuTriggerProps) => {
|
||||
const { children } = props;
|
||||
|
||||
const menuFloatingContext = useMenuFloatingContext();
|
||||
const { refs, setIsOpen, getReferenceProps } = menuFloatingContext!;
|
||||
|
||||
return (
|
||||
<MenuTriggerContext.Provider value={{ isTrigger: true }}>
|
||||
<Slot className={"reset"} ref={refs.setReference} {...getReferenceProps(
|
||||
{ onClick: () => { setIsOpen(true) } }
|
||||
)}>
|
||||
{children}
|
||||
</Slot>
|
||||
</MenuTriggerContext.Provider>
|
||||
)
|
||||
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
import {
|
||||
FloatingNode,
|
||||
FloatingTree,
|
||||
} from "@floating-ui/react";
|
||||
import type { ComponentPropsWithoutRef } from "react";
|
||||
import { useMenuFloating } from "./use-menu-floating";
|
||||
import { MenuFloatingContext } from "./use-menu-floating-context";
|
||||
import { MenuContext } from "./use-menu-context";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
|
||||
export type MenuProps = ComponentPropsWithoutRef<"div"> & {
|
||||
size?: "sm" | "md";
|
||||
};
|
||||
|
||||
export const Menu = (props: MenuProps) => {
|
||||
const { size = "sm", className = "", children, ...rest } = props;
|
||||
|
||||
|
||||
const menuFloatingContextValue = useMenuFloating();
|
||||
const { isNested, nodeId } = menuFloatingContextValue!
|
||||
|
||||
// ==========================================================================================
|
||||
|
||||
const rootClass = mergeClass("position--relative",className)
|
||||
|
||||
// ==========================================================================================
|
||||
|
||||
if (!isNested) {
|
||||
return (
|
||||
<MenuContext.Provider value={{ size }}>
|
||||
<MenuFloatingContext.Provider value={menuFloatingContextValue}>
|
||||
<FloatingTree>
|
||||
<FloatingNode id={nodeId}>
|
||||
<div {...rest} className={rootClass}>
|
||||
{children}
|
||||
</div>
|
||||
</FloatingNode>
|
||||
</FloatingTree>
|
||||
</MenuFloatingContext.Provider>
|
||||
</MenuContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<MenuContext.Provider value={{ size }}>
|
||||
<MenuFloatingContext.Provider value={menuFloatingContextValue}>
|
||||
<FloatingNode id={nodeId}>
|
||||
<div {...rest} className={rootClass} >
|
||||
{children}
|
||||
</div>
|
||||
</FloatingNode>
|
||||
</MenuFloatingContext.Provider>
|
||||
</MenuContext.Provider>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
export type MenuContextValue = { size?: "sm" | "md" } | null;
|
||||
|
||||
export const MenuContext = createContext<MenuContextValue>(null);
|
||||
|
||||
export function useMenuContext() {
|
||||
const menuContext = useContext(MenuContext);
|
||||
|
||||
return menuContext;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { createContext, useContext } from "react";
|
||||
import type { useMenuFloating } from "./use-menu-floating";
|
||||
|
||||
export type MenuFloatingContextValue = ReturnType<
|
||||
typeof useMenuFloating
|
||||
> | null;
|
||||
|
||||
export const MenuFloatingContext =
|
||||
createContext<MenuFloatingContextValue>(null);
|
||||
|
||||
export function useMenuFloatingContext() {
|
||||
const menuFloatingContext = useContext(MenuFloatingContext);
|
||||
|
||||
return menuFloatingContext;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import {
|
||||
autoUpdate,
|
||||
flip,
|
||||
offset,
|
||||
safePolygon,
|
||||
shift,
|
||||
useClick,
|
||||
useDismiss,
|
||||
useFloating,
|
||||
useFloatingNodeId,
|
||||
useFloatingParentNodeId,
|
||||
useHover,
|
||||
useInteractions,
|
||||
useListNavigation,
|
||||
useRole,
|
||||
} from "@floating-ui/react";
|
||||
import { useRef, useState } from "react";
|
||||
|
||||
export function useMenuFloating() {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [activeIndex, setActiveIndex] = useState<number | null>(null);
|
||||
const [selectedIndex, setSelectedIndex] = useState<number | null>(null);
|
||||
|
||||
const nodeId = useFloatingNodeId();
|
||||
const parentId = useFloatingParentNodeId();
|
||||
const isNested = parentId != null;
|
||||
|
||||
const elementsRef = useRef<Array<HTMLElement | null>>([]);
|
||||
|
||||
const { refs, floatingStyles, context } = useFloating({
|
||||
nodeId,
|
||||
placement: isNested ? "right-start" : "bottom-start",
|
||||
open: isOpen,
|
||||
onOpenChange: setIsOpen,
|
||||
whileElementsMounted: autoUpdate,
|
||||
middleware: [
|
||||
offset({
|
||||
mainAxis: isNested ? 0 : 4,
|
||||
alignmentAxis: isNested ? -4 : 0,
|
||||
}),
|
||||
flip(),
|
||||
shift(),
|
||||
],
|
||||
});
|
||||
|
||||
const hover = useHover(context, {
|
||||
enabled: isNested,
|
||||
delay: { open: 75 },
|
||||
handleClose: safePolygon({ blockPointerEvents: true }),
|
||||
});
|
||||
|
||||
const click = useClick(context, {
|
||||
event: "mousedown",
|
||||
toggle: !isNested,
|
||||
ignoreMouse: isNested,
|
||||
});
|
||||
|
||||
const role = useRole(context, { role: "menu" });
|
||||
|
||||
const dismiss = useDismiss(context, { bubbles: true });
|
||||
|
||||
const listNav = useListNavigation(context, {
|
||||
listRef: elementsRef,
|
||||
activeIndex,
|
||||
selectedIndex,
|
||||
nested: isNested,
|
||||
onNavigate: setActiveIndex,
|
||||
});
|
||||
|
||||
const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions(
|
||||
[hover, click, dismiss, role, listNav],
|
||||
);
|
||||
|
||||
return {
|
||||
nodeId,
|
||||
parentId,
|
||||
isNested,
|
||||
refs,
|
||||
floatingStyles,
|
||||
context,
|
||||
getReferenceProps,
|
||||
getFloatingProps,
|
||||
getItemProps,
|
||||
elementsRef,
|
||||
selectedIndex,
|
||||
setSelectedIndex,
|
||||
activeIndex,
|
||||
isOpen,
|
||||
setIsOpen
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
export type MenuRadioItemGroupContextValue = {
|
||||
size: "sm" | "md"
|
||||
selectName: string;
|
||||
setSelectName: React.Dispatch<React.SetStateAction<string>>;
|
||||
} | null;
|
||||
|
||||
export const MenuRadioItemGroupContext = createContext<MenuRadioItemGroupContextValue>(null)
|
||||
|
||||
export function useMenuRadioItemGroupContext() {
|
||||
return useContext(MenuRadioItemGroupContext)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
export type MenuTriggerContextValue = { isTrigger?: boolean } | null;
|
||||
|
||||
export const MenuTriggerContext = createContext<MenuTriggerContextValue>(null);
|
||||
|
||||
export function useMenuTriggerContext() {
|
||||
return useContext(MenuTriggerContext);
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import type { CSSProperties } from "react";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { usePopoverContext } from "./use-popover-context";
|
||||
import {
|
||||
FloatingArrow,
|
||||
FloatingFocusManager,
|
||||
FloatingPortal,
|
||||
} from "@floating-ui/react";
|
||||
import { useThemeContext } from "../theme/use-theme-context";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
import { variantRecipe } from "../../styles/recipes/variant-recipe";
|
||||
import { brandRecipe } from "../../styles/recipes/brand-recipe";
|
||||
import { containerRecipe } from "../../styles/recipes/container-recipe";
|
||||
|
||||
export type PopoverDropdownProps = CommonProps;
|
||||
|
||||
export const PopoverDropdown = (props: PopoverDropdownProps) => {
|
||||
const { children, className = "", style, ...rest } = props;
|
||||
|
||||
const {
|
||||
refs,
|
||||
floatingStyles,
|
||||
getFloatingProps,
|
||||
context,
|
||||
arrowRef,
|
||||
isMounted,
|
||||
transitionStyles,
|
||||
arrowWidth,
|
||||
arrowHeight,
|
||||
} = usePopoverContext()!;
|
||||
|
||||
const { themeClass, themeBaseClass } = useThemeContext()!;
|
||||
|
||||
const rootClass = mergeClass(
|
||||
"text--md",
|
||||
containerRecipe({
|
||||
boxShadow: true,
|
||||
rounded: 8,
|
||||
padding: 12,
|
||||
background: "base",
|
||||
hasFocusOutline: "false",
|
||||
}),
|
||||
brandRecipe(),
|
||||
themeBaseClass,
|
||||
themeClass,
|
||||
className,
|
||||
);
|
||||
|
||||
const rootStyle: CSSProperties = {
|
||||
...transitionStyles,
|
||||
...floatingStyles,
|
||||
...style,
|
||||
};
|
||||
|
||||
const arrowStyle: CSSProperties = {
|
||||
fill: "var(--base-background)",
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{isMounted && (
|
||||
<FloatingPortal>
|
||||
<FloatingFocusManager context={context} modal={false}>
|
||||
<div
|
||||
className={rootClass}
|
||||
ref={refs.setFloating}
|
||||
style={rootStyle}
|
||||
{...getFloatingProps({ ...rest })}
|
||||
>
|
||||
{children}
|
||||
<FloatingArrow
|
||||
ref={arrowRef}
|
||||
context={context}
|
||||
width={arrowWidth}
|
||||
height={arrowHeight}
|
||||
style={arrowStyle}
|
||||
></FloatingArrow>
|
||||
</div>
|
||||
</FloatingFocusManager>
|
||||
</FloatingPortal>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
import type { CommonProps } from "../../utils/CommonProps"
|
||||
import { Slot } from "../../utils/slot";
|
||||
import { usePopoverContext } from "./use-popover-context";
|
||||
|
||||
export type PopoverTriggerProps = CommonProps
|
||||
|
||||
export const PopoverTrigger = (props: PopoverTriggerProps) => {
|
||||
const { children, ...rest } = props;
|
||||
|
||||
const { refs, getReferenceProps, setIsOpen, isOpen } = usePopoverContext()!
|
||||
|
||||
return <Slot ref={refs.setReference} {...getReferenceProps(
|
||||
{ ...rest, onClick: () => { setIsOpen(!isOpen) } }
|
||||
)}>
|
||||
{children}
|
||||
</Slot>
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { CommonProps } from "../../utils/CommonProps"
|
||||
import { PopoverContext } from "./use-popover-context"
|
||||
import { usePopoverFloating } from "./use-popover-floating"
|
||||
|
||||
export type PopoverProps = CommonProps & {
|
||||
|
||||
}
|
||||
|
||||
export const Popover = (props: PopoverProps) => {
|
||||
const { children } = props
|
||||
const popoverFloatingContext = usePopoverFloating()
|
||||
|
||||
return <PopoverContext.Provider value={popoverFloatingContext}>
|
||||
<div>{children}</div>
|
||||
</PopoverContext.Provider>
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { createContext, useContext } from "react";
|
||||
import type { usePopoverFloating } from "./use-popover-floating";
|
||||
|
||||
export type PopoverContextValue = ReturnType<
|
||||
typeof usePopoverFloating
|
||||
> | null;
|
||||
|
||||
export const PopoverContext =
|
||||
createContext<PopoverContextValue>(null);
|
||||
|
||||
export function usePopoverContext() {
|
||||
const popoverContext = useContext(PopoverContext);
|
||||
|
||||
return popoverContext;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { arrow, autoUpdate, flip, offset, shift, useClick, useDismiss, useFloating, useInteractions, useRole, useTransitionStyles } from "@floating-ui/react";
|
||||
import { useMemo, useRef, useState } from "react";
|
||||
|
||||
export function usePopoverFloating() {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const arrowRef = useRef(null);
|
||||
|
||||
// 拉扯 svg 图像,让三角变得,更尖更长,或者更宽更扁
|
||||
const arrowWidth = 16
|
||||
const arrowHeight = 8
|
||||
|
||||
const { refs, floatingStyles, context, middlewareData } = useFloating({
|
||||
placement: "top-start",
|
||||
open: isOpen,
|
||||
onOpenChange: setIsOpen,
|
||||
whileElementsMounted: autoUpdate,
|
||||
middleware: [
|
||||
offset(arrowHeight),
|
||||
flip({ padding: 8 }),
|
||||
// 偏移完成后,再留 8px 空白,而不是紧贴着父容器
|
||||
shift({ padding: 8 }),
|
||||
arrow({
|
||||
element: arrowRef,
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
// 这两个是未经 useFloating 变换前的坐标
|
||||
// 也就是 arrow 悬浮,左对齐 floating,两者水平中线重合
|
||||
const arrowX = middlewareData.arrow?.x ?? 0;
|
||||
const arrowY = middlewareData.arrow?.y ?? 0;
|
||||
|
||||
// 变换中心的起始坐标就是 floating 的左上角
|
||||
const transformX = arrowX + arrowWidth / 2;
|
||||
const transformY = arrowY + arrowHeight;
|
||||
|
||||
// style 用于内容框,而不是 FloatingArrow
|
||||
const { isMounted, styles: transitionStyles } = useTransitionStyles(context, {
|
||||
duration: 150,
|
||||
initial: {
|
||||
opacity: 0,
|
||||
transform: "scale(0)"
|
||||
},
|
||||
close: {
|
||||
opacity: 0
|
||||
},
|
||||
common: ({ side }) => ({
|
||||
transitionTimingFunction: "ease",
|
||||
transformOrigin: {
|
||||
top: `${transformX}px calc(100% + ${arrowHeight}px)`,
|
||||
bottom: `${transformX}px ${-arrowHeight}px`,
|
||||
left: `calc(100% + ${arrowHeight}px) ${transformY}px`,
|
||||
right: `${-arrowHeight}px ${transformY}px`
|
||||
}[side]
|
||||
})
|
||||
})
|
||||
|
||||
const click = useClick(context);
|
||||
const dismiss = useDismiss(context);
|
||||
const role = useRole(context);
|
||||
|
||||
const { getReferenceProps, getFloatingProps } = useInteractions([
|
||||
click,
|
||||
dismiss,
|
||||
role,
|
||||
]);
|
||||
|
||||
return useMemo(() => ({
|
||||
isOpen,
|
||||
setIsOpen,
|
||||
/* only use at {isMounted && <FloatingPortal/>} */
|
||||
isMounted,
|
||||
arrowRef,
|
||||
arrowHeight,
|
||||
arrowWidth,
|
||||
refs,
|
||||
floatingStyles,
|
||||
transitionStyles,
|
||||
context,
|
||||
getReferenceProps,
|
||||
getFloatingProps
|
||||
}), [isOpen, setIsOpen, isMounted, refs, arrowHeight, arrowWidth, arrowRef, floatingStyles, transitionStyles, context, getReferenceProps, getFloatingProps])
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { CSSProperties } from "react";
|
||||
|
||||
export const Slider = () => {
|
||||
const sliderStyle: CSSProperties = {
|
||||
display: "flex",
|
||||
flexWrap: "nowrap",
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
userSelect: "none",
|
||||
};
|
||||
|
||||
const sliderThumbStyle: CSSProperties = {
|
||||
height: "16px",
|
||||
width: "16px",
|
||||
};
|
||||
|
||||
const sliderRailStyle: CSSProperties = {
|
||||
height: "6px",
|
||||
width: "auto",
|
||||
};
|
||||
|
||||
return <div></div>
|
||||
};
|
||||
@@ -0,0 +1,63 @@
|
||||
|
||||
import { Icon } from "../icon/icon";
|
||||
import { CloseSvg } from "../../assets/svg/CloseSvg";
|
||||
import { displayRecipe } from "../../styles/recipes/display-recipe";
|
||||
import { itemSizeRecipe } from "../../styles/recipes/item-size-recipe";
|
||||
import { brandRecipe } from "../../styles/recipes/brand-recipe";
|
||||
import { variantRecipe } from "../../styles/recipes/variant-recipe";
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { useState, type ReactNode } from "react";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
|
||||
export type TagProps = CommonProps & {
|
||||
size?: "xs" | "sm";
|
||||
variant?: "filled" | "outline";
|
||||
brand?: "success" | "danger" | "info" | "warning" | "default";
|
||||
color?: string;
|
||||
background?: string;
|
||||
borderColor?: string;
|
||||
} & {
|
||||
isClosable?: boolean;
|
||||
onClose?: () => void;
|
||||
};
|
||||
|
||||
export const Tag = (props: TagProps) => {
|
||||
const {
|
||||
className = "",
|
||||
children,
|
||||
size = "xs",
|
||||
brand = "info",
|
||||
color,
|
||||
background,
|
||||
borderColor,
|
||||
onClose,
|
||||
...rest
|
||||
} = props;
|
||||
|
||||
const rootClass = mergeClass(
|
||||
displayRecipe(),
|
||||
variantRecipe({ variant: "filled", isUnclickable: true }),
|
||||
itemSizeRecipe({ size, shape: 'rounded' }),
|
||||
className,
|
||||
);
|
||||
|
||||
const closeIconClase = mergeClass(
|
||||
variantRecipe({ variant: "ghost" }),
|
||||
brandRecipe({ brand: "default" }),
|
||||
"cursor--pointer"
|
||||
);
|
||||
|
||||
return (
|
||||
<div {...rest} className={rootClass} style={{ color: color, background: background, borderColor: borderColor }}>
|
||||
{children}
|
||||
<Icon
|
||||
className={closeIconClase}
|
||||
size={size}
|
||||
shape={"circle"}
|
||||
onClick={onClose}
|
||||
>
|
||||
<CloseSvg />
|
||||
</Icon>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
import { useState } from "react";
|
||||
|
||||
import type { CommonProps } from "../../utils/CommonProps";
|
||||
import { ThemeContext } from "./use-theme-context";
|
||||
import { mergeClass } from "../../styles/utils/merge-class";
|
||||
|
||||
export type ThemeProps = CommonProps & {
|
||||
theme?: "theme--light" | "theme--dark" | string;
|
||||
};
|
||||
|
||||
export const Theme = (props: ThemeProps) => {
|
||||
const { children, className = "", theme = "theme--light" } = props;
|
||||
|
||||
const [themeClass, setThemeClass] = useState<string>(theme);
|
||||
|
||||
const themeBaseClass = "theme-base";
|
||||
|
||||
const rootClass = mergeClass(themeBaseClass, theme, className);
|
||||
|
||||
return (
|
||||
<ThemeContext.Provider
|
||||
value={{ themeBaseClass, themeClass, setThemeClass }}
|
||||
>
|
||||
<div className={rootClass}>{children}</div>
|
||||
</ThemeContext.Provider>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
export type ThemeContextValue = {
|
||||
themeBaseClass: string;
|
||||
themeClass: string;
|
||||
setThemeClass: (v: string) => void;
|
||||
} | null;
|
||||
|
||||
export const ThemeContext = createContext<ThemeContextValue>(null);
|
||||
|
||||
export function useThemeContext() {
|
||||
const themeContext = useContext(ThemeContext);
|
||||
return themeContext;
|
||||
}
|
||||
Reference in New Issue
Block a user