2024-04-10 11:53:53 +02:00
|
|
|
import m, { Component, Vnode } from "mithril"
|
|
|
|
import { BaseButton } from "./BaseButton.js"
|
2024-09-02 15:31:12 +02:00
|
|
|
import { AllIcons, Icon, IconSize } from "../Icon.js"
|
2024-04-10 11:53:53 +02:00
|
|
|
import { ClickHandler } from "../GuiUtils.js"
|
|
|
|
import { AriaRole } from "../../AriaUtils.js"
|
|
|
|
import { theme } from "../../theme.js"
|
2025-01-14 18:12:43 +01:00
|
|
|
import { lang, MaybeTranslation } from "../../../misc/LanguageViewModel.js"
|
2024-04-10 11:53:53 +02:00
|
|
|
|
|
|
|
export interface RowButtonAttrs {
|
|
|
|
/** accessibility & tooltip description */
|
2025-01-14 18:12:43 +01:00
|
|
|
label: MaybeTranslation
|
2024-04-10 11:53:53 +02:00
|
|
|
/** visible text inside button */
|
2025-01-14 18:12:43 +01:00
|
|
|
text?: MaybeTranslation
|
2024-04-10 11:53:53 +02:00
|
|
|
icon?: AllIcons | "none"
|
|
|
|
selected?: boolean
|
|
|
|
onclick: ClickHandler
|
|
|
|
style?: Record<string, any>
|
|
|
|
class?: string
|
|
|
|
role?: AriaRole
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A button that is styled the same as a `NavButton`. */
|
|
|
|
export class RowButton implements Component<RowButtonAttrs> {
|
|
|
|
view(vnode: Vnode<RowButtonAttrs>) {
|
|
|
|
const attrs = vnode.attrs
|
2025-01-14 18:12:43 +01:00
|
|
|
const label = lang.getTranslationText(attrs.label)
|
|
|
|
const text = lang.getTranslationText(attrs.text ?? attrs.label)
|
2025-04-04 09:59:49 +02:00
|
|
|
const color = attrs.selected ? theme.primary : theme.on_surface_variant
|
2024-04-10 11:53:53 +02:00
|
|
|
return m(BaseButton, {
|
2025-01-14 18:12:43 +01:00
|
|
|
label: attrs.label,
|
2024-11-12 12:58:00 +01:00
|
|
|
text: m(
|
2025-10-17 15:06:50 +02:00
|
|
|
".plr-8.text-ellipsis",
|
2024-11-12 12:58:00 +01:00
|
|
|
{
|
|
|
|
style: { color },
|
|
|
|
// When the label doesn't match content, screen readers read both
|
|
|
|
ariaHidden: label !== text, // this prevents that
|
|
|
|
},
|
|
|
|
text,
|
|
|
|
),
|
2024-04-10 11:53:53 +02:00
|
|
|
role: attrs.role,
|
|
|
|
selected: attrs.selected,
|
|
|
|
icon:
|
|
|
|
attrs.icon && attrs.icon !== "none"
|
|
|
|
? m(Icon, {
|
|
|
|
icon: attrs.icon,
|
|
|
|
container: "div",
|
2025-10-10 14:14:45 +02:00
|
|
|
class: "mr-8",
|
2024-04-10 11:53:53 +02:00
|
|
|
style: { fill: color },
|
2025-09-05 15:04:29 +02:00
|
|
|
size: IconSize.PX24,
|
2025-07-07 11:51:45 +02:00
|
|
|
})
|
2024-04-10 11:53:53 +02:00
|
|
|
: attrs.icon === "none"
|
2025-10-10 14:14:45 +02:00
|
|
|
? m(".icon-24.mr-8")
|
2025-07-07 11:51:45 +02:00
|
|
|
: null,
|
2025-10-17 15:06:50 +02:00
|
|
|
class: "flex items-center state-bg button-content plr-8 " + attrs.class,
|
2024-04-10 11:53:53 +02:00
|
|
|
style: {
|
|
|
|
...attrs.style,
|
|
|
|
color,
|
|
|
|
},
|
|
|
|
onclick: attrs.onclick,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|