UNPKG

mui-tiptap

Version:

A Material-UI (MUI) styled WYSIWYG rich text editor, using Tiptap

67 lines (66 loc) 3.71 kB
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { styled, useThemeProps } from "@mui/material/styles"; import { clsx } from "clsx"; import { useState } from "react"; import { FormatColorBar } from "../icons"; import { getUtilityComponentName } from "../styles"; import { ColorPickerPopper } from "./ColorPickerPopper"; import MenuButton, { MENU_BUTTON_FONT_SIZE_DEFAULT, } from "./MenuButton"; import { menuButtonColorPickerClasses, } from "./MenuButtonColorPicker.classes"; const componentName = getUtilityComponentName("MenuButtonColorPicker"); // We can use an arbitrary component here ("span"), since we use `as` below with // the `IconComponent` prop. const MenuButtonColorPickerIcon = styled("span", { name: componentName, slot: "icon", overridesResolver: (props, styles) => styles.icon, })({ fontSize: MENU_BUTTON_FONT_SIZE_DEFAULT, }); const MenuButtonColorPickerIndicatorIcon = styled(FormatColorBar, { name: componentName, slot: "colorIndicatorIcon", overridesResolver: (props, styles) => [ styles.colorIndicatorIcon, props.ownerState.disabled && styles.colorIndicatorIconDisabled, ], })(({ theme, ownerState }) => ({ fontSize: MENU_BUTTON_FONT_SIZE_DEFAULT, position: "absolute", ...(ownerState.disabled && { color: theme.palette.action.disabled, }), })); export default function MenuButtonColorPicker(inProps) { const props = useThemeProps({ props: inProps, name: componentName }); const { value: colorValue, defaultPickerColor, onChange, swatchColors, labels, hideColorIndicator = false, popperId, PopperProps, ColorPickerProps, classes = {}, ...menuButtonProps } = props; const [anchorEl, setAnchorEl] = useState(null); const ownerState = { disabled: menuButtonProps.disabled, }; const handleClose = () => { setAnchorEl(null); }; const { IconComponent, children, ...otherMenuButtonProps } = menuButtonProps; return (_jsxs(_Fragment, { children: [_jsx(MenuButton, { onClick: (e) => { if (anchorEl) { handleClose(); } else { setAnchorEl(e.currentTarget); } }, "aria-describedby": popperId, ...otherMenuButtonProps, children: children !== null && children !== void 0 ? children : (_jsxs(_Fragment, { children: [IconComponent && (_jsx(MenuButtonColorPickerIcon, { as: IconComponent, ownerState: ownerState, className: clsx([ menuButtonColorPickerClasses.icon, classes.icon, ]) })), !hideColorIndicator && colorValue && (_jsx(MenuButtonColorPickerIndicatorIcon, { ownerState: ownerState, className: clsx([ menuButtonColorPickerClasses.colorIndicatorIcon, classes.colorIndicatorIcon, menuButtonProps.disabled && [ menuButtonColorPickerClasses.colorIndicatorIconDisabled, classes.colorIndicatorIconDisabled, ], ]), style: menuButtonProps.disabled ? undefined : { color: colorValue } }))] })) }), _jsx(ColorPickerPopper, { id: popperId, open: !!anchorEl, anchorEl: anchorEl, value: colorValue || defaultPickerColor || "", onSave: (newColor) => { onChange(newColor); handleClose(); }, onCancel: handleClose, swatchColors: swatchColors, ColorPickerProps: ColorPickerProps, labels: labels, ...PopperProps })] })); }