2023-06-28 17:35:28 +00:00
|
|
|
import { interactive, toggleable } from "../element"
|
2023-06-28 22:20:43 +00:00
|
|
|
import { background, foreground } from "../style_tree/components"
|
2023-07-04 05:20:56 +00:00
|
|
|
import { useTheme, Theme } from "../theme"
|
2023-06-27 20:20:45 +00:00
|
|
|
|
|
|
|
export type Margin = {
|
2023-06-28 17:35:28 +00:00
|
|
|
top: number
|
|
|
|
bottom: number
|
|
|
|
left: number
|
|
|
|
right: number
|
2023-06-27 20:20:45 +00:00
|
|
|
}
|
2023-06-27 15:34:12 +00:00
|
|
|
|
|
|
|
interface IconButtonOptions {
|
2023-08-02 19:15:39 +00:00
|
|
|
layer?:
|
|
|
|
| Theme["lowest"]
|
|
|
|
| Theme["middle"]
|
|
|
|
| Theme["highest"]
|
2023-07-04 05:20:56 +00:00
|
|
|
color?: keyof Theme["lowest"]
|
2023-06-28 17:35:28 +00:00
|
|
|
margin?: Partial<Margin>
|
2023-06-27 15:34:12 +00:00
|
|
|
}
|
|
|
|
|
2023-06-28 17:35:28 +00:00
|
|
|
type ToggleableIconButtonOptions = IconButtonOptions & {
|
2023-07-04 05:20:56 +00:00
|
|
|
active_color?: keyof Theme["lowest"]
|
2023-06-28 17:35:28 +00:00
|
|
|
}
|
2023-06-27 16:24:19 +00:00
|
|
|
|
2023-07-04 04:13:04 +00:00
|
|
|
export function icon_button({ color, margin, layer }: IconButtonOptions) {
|
|
|
|
const theme = useTheme()
|
|
|
|
|
2023-06-28 17:35:28 +00:00
|
|
|
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(
|
2023-07-04 05:20:56 +00:00
|
|
|
theme: Theme,
|
2023-06-28 17:35:28 +00:00
|
|
|
{ color, active_color, margin }: ToggleableIconButtonOptions
|
|
|
|
) {
|
|
|
|
if (!color) color = "base"
|
2023-06-27 16:24:19 +00:00
|
|
|
|
|
|
|
return toggleable({
|
|
|
|
state: {
|
2023-07-04 04:13:04 +00:00
|
|
|
inactive: icon_button({ color, margin }),
|
|
|
|
active: icon_button({
|
2023-06-28 17:35:28 +00:00
|
|
|
color: active_color ? active_color : color,
|
|
|
|
margin,
|
|
|
|
layer: theme.middle,
|
|
|
|
}),
|
|
|
|
},
|
2023-06-27 16:24:19 +00:00
|
|
|
})
|
|
|
|
}
|