zed/styles/src/component/button.ts

128 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-08-16 16:38:44 +00:00
import { font_sizes, useTheme } from "../common"
import { Layer, Theme } from "../theme"
import { TextStyle, background } from "../style_tree/components"
2023-08-14 19:13:57 +00:00
2023-08-16 16:38:44 +00:00
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Button {
export type Options = {
2023-08-29 20:02:04 +00:00
layer: Layer
2023-08-16 16:38:44 +00:00
background: keyof Theme["lowest"]
color: keyof Theme["lowest"]
variant: Button.Variant
size: Button.Size
shape: Button.Shape
margin: {
top?: number
bottom?: number
left?: number
right?: number
2023-08-29 20:02:04 +00:00
}
2023-08-16 16:38:44 +00:00
states: {
2023-08-29 20:02:04 +00:00
enabled?: boolean
hovered?: boolean
pressed?: boolean
focused?: boolean
disabled?: boolean
2023-08-16 16:38:44 +00:00
}
}
export type ToggleableOptions = Options & {
active_background: keyof Theme["lowest"]
active_color: keyof Theme["lowest"]
}
/** Padding added to each side of a Shape.Rectangle button */
export const RECTANGLE_PADDING = 2
export const FONT_SIZE = font_sizes.sm
export const ICON_SIZE = 14
export const CORNER_RADIUS = 6
export const variant = {
2023-08-29 20:02:04 +00:00
Default: "filled",
Outline: "outline",
Ghost: "ghost",
2023-08-16 16:38:44 +00:00
} as const
2023-08-29 20:02:04 +00:00
export type Variant = (typeof variant)[keyof typeof variant]
2023-08-16 16:38:44 +00:00
export const shape = {
2023-08-29 20:02:04 +00:00
Rectangle: "rectangle",
Square: "square",
2023-08-16 16:38:44 +00:00
} as const
2023-08-29 20:02:04 +00:00
export type Shape = (typeof shape)[keyof typeof shape]
2023-08-16 16:38:44 +00:00
export const size = {
Small: "sm",
2023-08-29 20:02:04 +00:00
Medium: "md",
2023-08-16 16:38:44 +00:00
} as const
2023-08-29 20:02:04 +00:00
export type Size = (typeof size)[keyof typeof size]
2023-08-16 16:38:44 +00:00
export type BaseStyle = {
corder_radius: number
background: string | null
padding: {
top: number
bottom: number
left: number
right: number
2023-08-29 20:02:04 +00:00
}
margin: Button.Options["margin"]
2023-08-16 16:38:44 +00:00
button_height: number
}
export type LabelButtonStyle = BaseStyle & TextStyle
// export type IconButtonStyle = ButtonStyle
export const button_base = (
options: Partial<Button.Options> = {
variant: Button.variant.Default,
shape: Button.shape.Rectangle,
states: {
hovered: true,
2023-08-29 20:02:04 +00:00
pressed: true,
},
2023-08-16 16:38:44 +00:00
}
): BaseStyle => {
const theme = useTheme()
const layer = options.layer ?? theme.middle
const color = options.color ?? "base"
2023-08-29 20:02:04 +00:00
const background_color =
options.variant === Button.variant.Ghost
? null
: background(layer, options.background ?? color)
2023-08-16 16:38:44 +00:00
const m = {
top: options.margin?.top ?? 0,
bottom: options.margin?.bottom ?? 0,
left: options.margin?.left ?? 0,
right: options.margin?.right ?? 0,
}
const size = options.size || Button.size.Medium
const padding = 2
const base: BaseStyle = {
background: background_color,
corder_radius: Button.CORNER_RADIUS,
padding: {
top: padding,
bottom: padding,
2023-08-29 20:02:04 +00:00
left:
options.shape === Button.shape.Rectangle
? padding + Button.RECTANGLE_PADDING
: padding,
right:
options.shape === Button.shape.Rectangle
? padding + Button.RECTANGLE_PADDING
: padding,
2023-08-16 16:38:44 +00:00
},
margin: m,
button_height: 16,
}
return base
}
}