zed/styles/src/component/icon_button.ts

87 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-06-28 17:35:28 +00:00
import { ColorScheme } from "../common"
import { interactive, toggleable } from "../element"
import { background, foreground } from "../styleTree/components"
export type Margin = {
2023-06-28 17:35:28 +00:00
top: number
bottom: number
left: number
right: number
}
2023-06-27 15:34:12 +00:00
interface IconButtonOptions {
2023-06-28 17:35:28 +00:00
layer?:
| ColorScheme["lowest"]
| ColorScheme["middle"]
| ColorScheme["highest"]
color?: keyof ColorScheme["lowest"]
margin?: Partial<Margin>
2023-06-27 15:34:12 +00:00
}
2023-06-28 17:35:28 +00:00
type ToggleableIconButtonOptions = IconButtonOptions & {
active_color?: keyof ColorScheme["lowest"]
}
2023-06-27 16:24:19 +00:00
2023-06-28 17:35:28 +00:00
export function icon_button(
theme: ColorScheme,
{ color, margin, layer }: IconButtonOptions
) {
if (!color) color = "base"
2023-06-27 15:34:12 +00:00
const m = {
top: margin?.top ?? 0,
bottom: margin?.bottom ?? 0,
left: margin?.left ?? 0,
right: margin?.right ?? 0,
}
return interactive({
base: {
2023-06-27 16:24:19 +00:00
corner_radius: 6,
2023-06-27 15:34:12 +00:00
padding: {
top: 2,
bottom: 2,
left: 4,
right: 4,
},
margin: m,
2023-06-28 14:19:30 +00:00
icon_width: 14,
icon_height: 14,
button_width: 20,
button_height: 16,
2023-06-27 15:34:12 +00:00
},
state: {
default: {
2023-06-27 16:24:19 +00:00
background: background(layer ?? theme.lowest, color),
color: foreground(layer ?? theme.lowest, color),
2023-06-27 15:34:12 +00:00
},
hovered: {
2023-06-27 16:24:19 +00:00
background: background(layer ?? theme.lowest, color, "hovered"),
color: foreground(layer ?? theme.lowest, color, "hovered"),
2023-06-27 15:34:12 +00:00
},
clicked: {
2023-06-27 16:24:19 +00:00
background: background(layer ?? theme.lowest, color, "pressed"),
color: foreground(layer ?? theme.lowest, color, "pressed"),
2023-06-27 15:34:12 +00:00
},
},
2023-06-28 17:35:28 +00:00
})
2023-06-27 15:34:12 +00:00
}
2023-06-27 16:24:19 +00:00
2023-06-28 17:35:28 +00:00
export function toggleable_icon_button(
theme: ColorScheme,
{ color, active_color, margin }: ToggleableIconButtonOptions
) {
if (!color) color = "base"
2023-06-27 16:24:19 +00:00
return toggleable({
state: {
inactive: icon_button(theme, { color, margin }),
2023-06-28 17:35:28 +00:00
active: icon_button(theme, {
color: active_color ? active_color : color,
margin,
layer: theme.middle,
}),
},
2023-06-27 16:24:19 +00:00
})
}