Files
heroui-vue/tests/package/color-slider-contract.ts
T

264 lines
7.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { h } from "vue";
import {
ColorSlider,
ColorSliderOutput,
ColorSliderRoot,
ColorSliderThumb,
ColorSliderTrack,
colorSliderVariants,
parseColor,
type AlphaChannel,
type Color,
type ColorChannel,
type ColorSliderBaseProps,
type ColorSliderChannelProps,
type ColorSliderExpose,
type ColorSliderOutputProps,
type ColorSliderOutputRenderProps,
type ColorSliderOutputSlots,
type ColorSliderOutputStyle,
type ColorSliderOutputStyleFn,
type ColorSliderProps,
type ColorSliderRenderProps,
type ColorSliderRootProps,
type ColorSliderSlots,
type ColorSliderStyle,
type ColorSliderStyleFn,
type ColorSliderThumbProps,
type ColorSliderThumbRenderProps,
type ColorSliderThumbSlots,
type ColorSliderThumbStyle,
type ColorSliderThumbStyleFn,
type ColorSliderTrackProps,
type ColorSliderTrackRenderProps,
type ColorSliderTrackSlots,
type ColorSliderTrackStyle,
type ColorSliderTrackStyleFn,
type ColorSliderVariants,
type ColorSpace,
type ColorValue,
type HSLChannel,
type HSLHSBSharedChannel,
type HSBChannel,
type RGBChannel,
} from "@heroui-vue/components";
const color = parseColor("#9B80FF");
const value: ColorValue = color;
const channel: ColorChannel = "hue";
const space: ColorSpace = "hsb";
const hslChannel: HSLChannel = "lightness";
const hsbChannel: HSBChannel = "brightness";
const rgbChannel: RGBChannel = "red";
const sharedChannel: HSLHSBSharedChannel = "saturation";
const alphaChannel: AlphaChannel = "alpha";
const style: ColorSliderStyle = { outline: "1px solid black" };
const styleFn: ColorSliderStyleFn = ({ color: current }) => ({
outline: `1px solid ${current.toString("hex")}`,
});
const styleRender: ColorSliderStyle = ({ channel: active }) => ({
opacity: active ? 0.8 : 1,
});
const outputStyle: ColorSliderOutputStyle = {
fontVariantNumeric: "tabular-nums",
};
const outputStyleFn: ColorSliderOutputStyleFn = ({ label }) => ({
width: `${label.length}ch`,
});
const trackStyle: ColorSliderTrackStyle = { borderColor: "black" };
const trackStyleFn: ColorSliderTrackStyleFn = ({ percent }) => ({
opacity: percent,
});
const thumbStyle: ColorSliderThumbStyle = { cursor: "grab" };
const thumbStyleFn: ColorSliderThumbStyleFn = ({ isDragging }) => ({
width: isDragging ? "20px" : "16px",
});
const base: ColorSliderBaseProps = {
modelValue: color,
value,
defaultValue: "hsb(240, 50%, 50%)",
orientation: "vertical",
name: "hue",
form: "color-form",
isDisabled: false,
disabled: false,
isReadOnly: false,
locale: "zh-CN",
dir: "rtl",
as: "div",
asChild: false,
style,
styleFn,
id: "hue-slider",
};
const channelProps: ColorSliderChannelProps = {
channel: "hue",
colorSpace: "hsb",
};
const props: ColorSliderProps = { ...base, ...channelProps };
const rootProps: ColorSliderRootProps = props;
const outputProps: ColorSliderOutputProps = {
as: "output",
style: outputStyle,
styleFn: outputStyleFn,
};
const trackProps: ColorSliderTrackProps = {
as: "div",
style: trackStyle,
styleFn: trackStyleFn,
};
const thumbProps: ColorSliderThumbProps = {
as: "span",
style: thumbStyle,
styleFn: thumbStyleFn,
};
const render: ColorSliderRenderProps = {
color,
channel: "hue",
colorSpace: "hsb",
orientation: "vertical",
isDisabled: false,
isReadOnly: false,
};
const outputRender: ColorSliderOutputRenderProps = {
...render,
value: 240,
label: "240°",
};
const trackRender: ColorSliderTrackRenderProps = { ...render, percent: 0.5 };
const thumbRender: ColorSliderThumbRenderProps = {
...trackRender,
isDragging: true,
};
const slots: ColorSliderSlots = { default: () => null };
const outputSlots: ColorSliderOutputSlots = { default: () => null };
const trackSlots: ColorSliderTrackSlots = { default: () => null };
const thumbSlots: ColorSliderThumbSlots = { default: () => null };
const variants: ColorSliderVariants = {};
const exposed: ColorSliderExpose = {
element: null,
color: null,
focus: () => {},
};
const typedColor: Color = color;
type ColorSliderInstanceProps = InstanceType<typeof ColorSlider>["$props"];
const instanceProps: ColorSliderInstanceProps = {
channel: "hue",
colorSpace: "hsb",
};
// @ts-expect-error the component's own props reject red + hsl
const badInstanceRed: ColorSliderInstanceProps = {
channel: "red",
colorSpace: "hsl",
};
// @ts-expect-error the component's own props reject hue + rgb
const badInstanceHue: ColorSliderInstanceProps = {
channel: "hue",
colorSpace: "rgb",
};
const sliderNode = h(ColorSlider, {
channel: "hue",
colorSpace: "hsb",
defaultValue: "hsb(240, 50%, 50%)",
});
// @ts-expect-error h() rejects red + hsl on the real component
h(ColorSlider, { channel: "red", colorSpace: "hsl" });
// @ts-expect-error h() rejects hue + rgb on the real component
h(ColorSlider, { channel: "hue", colorSpace: "rgb" });
// @ts-expect-error red requires the rgb color space
const badRed: ColorSliderProps = { channel: "red", colorSpace: "hsl" };
// @ts-expect-error hue is not available in the rgb color space
const badHue: ColorSliderProps = { channel: "hue", colorSpace: "rgb" };
// @ts-expect-error brightness requires the hsb color space
const badBrightness: ColorSliderProps = {
channel: "brightness",
colorSpace: "hsl",
};
// @ts-expect-error lightness requires the hsl color space
const badLightness: ColorSliderProps = {
channel: "lightness",
colorSpace: "hsb",
};
// @ts-expect-error blue requires the rgb color space
const badBlue: ColorSliderProps = { channel: "blue", colorSpace: "hsb" };
// @ts-expect-error channel is required
const badMissing: ColorSliderProps = {};
// @ts-expect-error the public channel set is finite
const badChannel: ColorSliderProps = { channel: "cyan" };
// @ts-expect-error the public space set is rgb | hsl | hsb
const badSpace: ColorSliderProps = { channel: "alpha", colorSpace: "lab" };
const badRender: ColorSliderRenderProps = {
// @ts-expect-error render props require a Color instance
color: "#fff",
channel: "hue",
colorSpace: "hsl",
orientation: "horizontal",
isDisabled: false,
isReadOnly: false,
};
const badThumb: ColorSliderThumbRenderProps = {
...render,
// @ts-expect-error percent is a 0–1 number, not a string
percent: "50%",
isDragging: false,
};
const badExpose: ColorSliderExpose = {
element: null,
color: null,
focus: () => {},
// @ts-expect-error channel is not part of the exposed instance
channel: "hue",
};
// @ts-expect-error style functions return Vue style values, not numbers
const badStyle: ColorSliderStyleFn = () => 1;
void [
ColorSlider,
ColorSliderRoot,
ColorSliderOutput,
ColorSliderTrack,
ColorSliderThumb,
colorSliderVariants,
value,
channel,
space,
hslChannel,
hsbChannel,
rgbChannel,
sharedChannel,
alphaChannel,
styleRender,
props,
rootProps,
outputProps,
trackProps,
thumbProps,
outputRender,
trackRender,
thumbRender,
slots,
outputSlots,
trackSlots,
thumbSlots,
variants,
exposed,
typedColor,
instanceProps,
badInstanceRed,
badInstanceHue,
sliderNode,
badRed,
badHue,
badBrightness,
badLightness,
badBlue,
badMissing,
badChannel,
badSpace,
badRender,
badThumb,
badExpose,
badStyle,
];