Button
A button with a text, an icon, or both. A click fires its click event, or opens its menu; it can repeat while held down.
import { Button } from "x4js"
Icons
The icon alone, with a tooltip, then the icon and the text.
export function icons( ) {
return new HBox( { content: [
new Button( { icon: icon_save, tooltip: "Save" } ),
new Button( { icon: icon_heart, cls: "round", tooltip: "Like" } ),
new Button( { icon: icon_save, label: "Save" } ),
new Button( { icon: icon_trash, label: "Delete" } ),
] } );
}
.x4button.round {
width: 36px;
height: 36px;
border-radius: 50%;
justify-content: center;
}
Styles
The classes outline and danger, and a class of your own that sets the CSS variables of the button.
export function styles( ) {
return new HBox( { content: [
new Button( { label: "Default" } ),
new Button( { label: "Outline", cls: "outline" } ),
new Button( { label: "Danger", cls: "danger" } ),
new Button( { label: "Green", cls: "green" } ),
] } );
}
.x4button.green {
--button-background: var( --color-green-700 );
--button-background-hover: var( --color-green-600 );
--button-background-focus: var( --color-green-800 );
--button-background-active: var( --color-green-900 );
}
Disabled
A disabled button cannot be clicked nor focused; enable() gives it back.
export function disabled( ) {
const target = new Button( { label: "Send", disabled: true } );
return new HBox( { content: [
target,
new Button( { label: "Enable / disable", cls: "outline", click: ( ) => {
target.enable( !!target.isDisabled( ) );
} } ),
] } );
}
Click
A click updates a label. With autorepeat, holding the button down clicks again and again.
export function click( ) {
let count = 0;
const label = new Label( { text: "Not clicked yet" } );
return new HBox( { content: [
new Button( { label: "Click me", click: ( ) => {
count++;
label.setText( `Clicked ${count} times` );
} } ),
new Button( { icon: icon_plus, label: "Hold me", autorepeat: true, click: ( ev ) => {
count++;
label.setText( `Clicked ${count} times (repeat ${ev.repeat ?? 0})` );
} } ),
label,
] } );
}
With a menu
A click opens the menu under the button, in place of the click event. Given as a function, the menu is built at each click: its entry counts the opens.
export function withMenu( ) {
let opened = 0;
const label = new Label( { text: "" } );
return new HBox( { content: [
new Button( { label: "File", menu: [
{ text: "Open", click: ( ) => label.setText( "Open" ) },
{ text: "Save", icon: icon_save, click: ( ) => label.setText( "Save" ) },
"-",
{ text: "Delete", icon: icon_trash, click: ( ) => label.setText( "Delete" ) },
] } ),
new Button( { label: "Recent", cls: "outline", menu: ( ) => {
opened++;
return [ { text: `Opened ${opened} times`, click: ( ) => label.setText( `Recent, open ${opened}` ) } ];
} } ),
label,
] } );
}
In a BtnGroup
Predefined buttons ("ok", "cancel": their text follows the language), a spacer, a button of your own; btnclick gives the name of the one clicked.
export function inBtnGroup( ) {
const label = new Label( { text: "" } );
return new HBox( { content: [
new BtnGroup( { width: 380, items: [
"ok",
"cancel.outline",
"-",
new Button( { id: "help", label: "Help", cls: "outline" } ),
], btnclick: ( ev ) => label.setText( `btnclick: ${ev.button}` ) } ),
label,
] } );
}
For an assistant: llms.txt, llms-full.txt.