x4js gallery

Popup

The base of what floats above the page: menus, dialogs, notifications. It is placed near a rectangle, at a point, or in the middle; it can close by itself on a click outside.

import { Popup } from "x4js"

displayNear places the popup against a rectangle; with autoClose, a click outside closes it.

export function near( ) {
	const button = new Button( { label: "Open near me", click: ( ) => {
		const popup = new Popup( { cls: "popup-panel", autoClose: true, content: [
			new SimpleText( { text: "Under the button, its left edge on the left of the button." } ),
			new SimpleText( { text: "Click outside to close it." } ),
		] } );

		popup.displayNear( button.getBoundingRect( ), "top left", "bottom left", { x: 0, y: 4 } );
	} } );

	return new HBox( { content: [ button ] } );
}
// a popup has no background of its own: the class gives it one
.x4popup.popup-panel {
	display: flex;
	flex-direction: column;
	gap: 8px;
	padding: 12px 16px;
	border: 1px solid var( --border );
	background-color: var( --background-primary );
	color: var( --text-primary );
	box-shadow: 0 8px 24px rgb( 0 0 0 / 20% );

	> .caption-element {
		cursor: move;
		font-weight: 600;
		--label-color: var( --text-primary );
	}
}

displayAt places it at a point: here a menu of your own, opened by a right click in the zone.

export function at( ) {
	const zone = new Component( { cls: "popup-zone", content: "Right click here", dom_events: {
		contextmenu: ( ev: MouseEvent ) => {
			ev.preventDefault( );

			const popup = new Popup( { cls: "popup-panel", autoClose: true, content: [
				new SimpleText( { text: `At x ${ev.pageX}, y ${ev.pageY}` } ),
			] } );

			popup.displayAt( ev.pageX, ev.pageY );
		},
	} } );

	return zone;
}
.popup-zone {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 120px;
	border: 2px dashed var( --border );
	color: var( --text-secondary );
}

A popup in the middle of the page: its title (class caption-element) moves it, its edges size it.

export function movable( ) {
	return new HBox( { content: [
		new Button( { label: "Open in the middle", click: ( ) => {
			const popup: Popup = new Popup( { cls: "popup-panel", movable: true, sizable: true, content: [
				new Label( { cls: "caption-element", text: "Drag me by this title" } ),
				new SimpleText( { flex: 1, text: "Drag an edge or a corner to size the popup." } ),
				new Button( { label: "Close", click: ( ) => popup.close( ) } ),
			] } );

			// show( ) is displayCenter( )
			popup.show( );
		} } ),
	] } );
}

The events of a popup: here a label tells whether it is open.

export function events( ) {
	const state = new Label( { text: "closed" } );

	const button = new Button( { label: "Open", click: ( ) => {
		const popup = new Popup( { cls: "popup-panel", autoClose: true, content: [
			new SimpleText( { text: "Click outside to close it." } ),
		] } );

		popup.on( "opened", ( ) => state.setText( "opened" ) );
		popup.on( "closed", ( ) => state.setText( "closed" ) );
		popup.displayNear( button.getBoundingRect( ), "top left", "top right", { x: 8, y: 0 } );
	} } );

	return new HBox( { content: [ button, state ] } );
}

For an assistant: llms.txt, llms-full.txt.