Link
A link (an <a>) with a text and an icon, or any content. Its click event comes before the browser follows it.
import { Link } from "x4js"
Text and icon
A link shows its text, underlined, after its icon; attrs gives it a target.
export function textAndIcon( ) {
return new VBox( { content: [
new Link( { href: "https://x4js.org", text: "x4js.org, in a new tab", icon: icon_home, attrs: { target: "_blank" } } ),
new Link( { href: "mailto:someone@example.com", text: "Write to us", icon: icon_mail } ),
] } );
}
Click
click is fired first; ev.context is the href, and ev.preventDefault( ) keeps the link from being followed.
export function click( ) {
const label = new Label( { text: "" } );
return new HBox( { content: [
new Link( { href: "https://example.com/report.pdf", text: "report.pdf", icon: icon_file, click: ( ev ) => {
ev.preventDefault( );
label.setText( `not followed: ${ev.context}` );
} } ),
label,
] } );
}
Content
With content, the link is what it holds: here a small card.
export function content( ) {
return new Link( { cls: "card-link", href: "#/link", content: [
new Icon( { iconId: icon_file } ),
new VBox( { flex: 1, content: [
new SimpleText( { cls: "title", text: "Getting started" } ),
new SimpleText( { text: "Create a project and run it in a few minutes." } ),
] } ),
new Icon( { iconId: icon_right } ),
] } );
}
.x4link.card-link {
display: flex;
align-items: center;
gap: 12px;
width: 360px;
padding: 12px;
border: 1px solid var( --border );
color: var( --text-primary );
text-decoration: none;
&:hover {
border-color: var( --accent-background );
}
.x4icon {
width: 20px;
height: 20px;
fill: currentColor;
}
.title {
font-weight: 600;
--label-color: var( --text-primary );
}
}
setText
setText changes the text of the link; its icon stays.
export function setText( ) {
let count = 0;
const link = new Link( { href: "#/link", text: "Clicked 0 times", icon: icon_right } );
link.on( "click", ( ev ) => {
ev.preventDefault( );
count++;
link.setText( `Clicked ${count} times` );
} );
return link;
}
For an assistant: llms.txt, llms-full.txt.