Tooltips
A text that shows under the pointer, on any component that has a tooltip. initTooltips( ) once at the start of the application turns them on.
import { initTooltips } from "x4js"
The tooltip prop
Any component takes a tooltip; it is shown under the pointer once initTooltips( ) was called at the start.
export function prop( ) {
return new HBox( { content: [
new Button( { icon: icon_save, tooltip: "Save the document" } ),
new Button( { icon: icon_trash, cls: "danger", tooltip: "Delete it" } ),
new Label( { icon: icon_info, text: "Hover me", tooltip: "A label has one too" } ),
new Input( { type: "text", placeholder: "a field", tooltip: "And a field" } ),
] } );
}
HTML
The text of a tooltip is HTML, styled by the page: a title, a sentence, a shortcut written by shortcutText, a list, a badge. A text that comes from a user is escaped with sanitizeHtml.
export function html( ) {
return new HBox( { content: [
new Button( { label: "Save", tooltip: `<div class="demo-tip">
<b>Save the document</b>
<p>Your changes go to the disk; the previous version is kept for 30 days.</p>
<span class="key">${shortcutText( "Mod+S" )}</span>
</div>` } ),
new Button( { label: "Export", cls: "outline", tooltip: `<div class="demo-tip">
<b>Export the table</b>
<ul>
<li><b>PDF</b>, to print it</li>
<li><b>CSV</b>, for a spreadsheet</li>
<li><b>JSON</b>, for the code</li>
</ul>
</div>` } ),
new Button( { label: "Publish", cls: "outline", tooltip: `<div class="demo-tip">
<b>Publish</b> <span class="badge">new</span>
<p>Your page online in one click, at <u>x4js.org</u>.</p>
<span class="muted">Last published 2 hours ago</span>
</div>` } ),
] } );
}
// the HTML of a tooltip takes the style of the page: a width to wrap its sentences
.demo-tip {
max-width: 260px;
white-space: normal;
line-height: 1.4;
p, ul {
margin: 4px 0;
}
ul {
padding-left: 18px;
}
// a key of the keyboard
.key {
display: inline-block;
padding: 0 6px;
border: 1px solid var( --border );
border-bottom-width: 2px;
border-radius: 4px;
background-color: var( --background-primary );
font-size: 85%;
}
.badge {
padding: 0 6px;
border-radius: 8px;
background-color: var( --success-background );
color: var( --success-color );
font-size: 80%;
}
.muted {
color: var( --text-secondary );
font-size: 85%;
}
}
Changing it
The tooltip is the attribute "tooltip": setAttribute changes it.
export function change( ) {
const target = new Button( { label: "Hover me", cls: "outline", tooltip: "Not checked" } );
return new HBox( { content: [
new Checkbox( { label: "Checked", change: ( ev ) => {
target.setAttribute( "tooltip", ev.value ? "Checked" : "Not checked" );
} } ),
target,
] } );
}
Computed when shown
A component that answers queryInterface( "tip-handler" ) gives its text at the moment it is shown.
export function handler( ) {
class Clock extends Component {
override queryInterface<T extends IComponentInterface>( name: string ): T {
if( name == "tip-handler" ) {
const tip: ITipHandler = { getTip: ( ) => `It is ${new Date( ).toLocaleTimeString( )}` };
return tip as unknown as T;
}
return super.queryInterface( name );
}
}
return new HBox( { content: [
new Clock( { cls: "clock", content: "What time is it?" } ),
] } );
}
.clock {
padding: 8px;
border-bottom: 1px dashed var( --text-secondary );
cursor: help;
}
For an assistant: llms.txt, llms-full.txt.