Elementi
Tooltip
Un tooltip è un breve suggerimento accanto all’elemento a cui si riferisce: cosa fa un pulsante con icona, il testo completo di un’etichetta troncata.
Sui desktop e nei browser desktop viene mostrato quando il puntatore resta fermo sul suo trigger per delay (500 ms), e
si nasconde quando il puntatore esce o preme il trigger. Telefoni e tablet mostrano un tooltip solo quando lo apre l’app
(open). Non cattura mai il puntatore né le pressioni, quindi il trigger funziona esattamente come funzionerebbe senza di esso.
import { mount, signal, Text, View } from "nsh";
import { type OpenChangeReason, Tooltip } from "nsh/base";
import { button, buttonText, createPresence, desktop, Inspector, Log, Panel, Stage, ui, wide } from "./palette";
/** An icon button with a hint of the app's look, above or below it. */
function Tool(props: {
icon: string;
label: string;
hint: string;
placement: "top" | "bottom";
pinned: boolean;
onPress: () => void;
onOpenChange: (e: { open: boolean; reason: OpenChangeReason }) => void;
}) {
const hovered = signal(false);
// The bubble stays made between showings: it comes in from its side as it shows.
const presence = createPresence(props.placement);
return (
<View style={{ alignItems: "center", gap: 6 }}>
<View
onPress={props.onPress}
accessibilityLabel={props.label}
{...button("secondary", { align: "center", style: { width: desktop ? 36 : 52, height: desktop ? 36 : 52, paddingHorizontal: 0 } })}
tooltip={
<Tooltip
open={props.pinned || hovered()}
placement={props.placement}
offset={8}
margin={8}
delay={400}
onOpenChange={(e) => {
hovered.set(e.open);
props.onOpenChange(e);
}}
onOpen={presence.show}
onClose={presence.hide}
>
<View style={{ backgroundColor: ui().text, borderRadius: 7, paddingHorizontal: 10, paddingVertical: 6, ...presence.style }}>
<Text style={{ fontSize: 13, color: ui().page }}>{props.hint}</Text>
</View>
</Tooltip>
}
>
<Text style={{ fontSize: desktop ? 17 : 22, color: ui().text }}>{props.icon}</Text>
</View>
<Text style={{ fontSize: 11, color: ui().faint }}>{props.placement}</Text>
</View>
);
}
function App() {
const events = signal<string[]>([]);
const pressed = signal("—");
const pinned = signal(false);
const log = (name: string) => ({ open, reason }: { open: boolean; reason: OpenChangeReason }) =>
events.set([`${name} ${open ? "shown" : "hidden"} · ${reason}`, ...events()].slice(0, 6));
const tools = () => (
<>
<Tool icon="★" label="Star" hint="Add to favorites" placement="top" pinned={pinned()} onPress={() => pressed.set("Star")} onOpenChange={log("Star")} />
<Tool icon="↗" label="Share" hint="Share with your team" placement="bottom" pinned={pinned()} onPress={() => pressed.set("Share")} onOpenChange={log("Share")} />
<Tool icon="✕" label="Delete" hint="Move to the trash" placement="top" pinned={pinned()} onPress={() => pressed.set("Delete")} onOpenChange={log("Delete")} />
</>
);
// The `tooltip` prop of any element: a `Tooltip` with the app's look.
const pin = () => (
<View
tooltip={
<Tooltip style={{ backgroundColor: ui().text, borderRadius: 7, paddingHorizontal: 10, paddingVertical: 6 }}>
<Text style={{ fontSize: 13, color: ui().page }}>Shows every tooltip without a pointer</Text>
</Tooltip>
}
onPress={() => pinned.set(!pinned())}
{...button("secondary", { align: "center", style: wide() ? {} : { flex: 1 } })}
>
<Text style={buttonText()}>
{wide() ? (pinned() ? "Unpin the tooltips" : "Pin the tooltips") : pinned() ? "Unpin" : "Pin"}
</Text>
</View>
);
const onPress = () => (
<Panel title="onPress">
<Text style={{ fontSize: 13, color: ui().text }}>{pressed()}</Text>
</Panel>
);
const changes = () => <Log title="onOpenChange({ open, reason })" lines={events()} minHeight={130} />;
return (
<View style={{ flex: 1, backgroundColor: ui().page, padding: 20, paddingTop: 24, gap: 14 }}>
<Text style={{ fontSize: 22, fontWeight: "700", color: ui().text }}>tooltip</Text>
<Text style={{ color: ui().muted }}>
{desktop ? "Rest the pointer on a button; a click still presses it" : "Tooltips follow a pointer: pin them to see them here"}
</Text>
{() =>
wide() ? (
<View style={{ flex: 1, flexDirection: "row", gap: 16 }}>
<Stage gap={40}>
<View style={{ flexDirection: "row", justifyContent: "center", gap: 64 }}>{tools()}</View>
{pin()}
</Stage>
<Inspector>
{onPress()}
{changes()}
</Inspector>
</View>
) : (
<>
<View style={{ flex: 1, minHeight: 200, flexDirection: "row", alignItems: "center", justifyContent: "center", gap: 28 }}>{tools()}</View>
<View style={{ flexDirection: "row", gap: 8 }}>{pin()}</View>
{onPress()}
{changes()}
</>
)
}
</View>
);
}
mount(App);Tooltip di nsh/base viene passato come prop tooltip che ogni elemento possiede
(Prop comuni), come .tooltip(..) di GPUI: l’elemento è ciò su cui si ferma il puntatore e ciò rispetto a cui
viene posizionato. Il tooltip vero e proprio è il fumetto: puoi stilizzarlo con class / style, e i suoi figli sono ciò che dice.
use nsh_base::tooltip;
use nsh_core::prelude::*;
fn save_button(on_save: impl Fn() + 'static) -> impl IntoElement {
view()
.on_press(on_save)
.accessibility_label("Save")
.p_2()
.child(text("💾"))
.tooltip(
tooltip()
.placement(Placement::Bottom)
.rounded_md()
.bg(rgb(0x111827))
.px_2()
.py_1()
.child(text("Save the document").text_color(white())),
)
}import { Text, View } from "nsh";
import { Tooltip } from "nsh/base";
function SaveButton(props: { onSave: () => void }) {
return (
<View
onPress={props.onSave}
accessibilityLabel="Save"
class="p-2"
tooltip={
<Tooltip placement="bottom" class="rounded-md bg-gray-900 px-2 py-1">
<Text class="text-white">Save the document</Text>
</Tooltip>
}
>
<Text>💾</Text>
</View>
);
}Per impostazione predefinita si posiziona sopra il trigger (placement), sull’altro lato quando manca spazio, e resta all’interno della
safe area. Con open è l’app a decidere quando mostrarlo: segue onOpenChange, che segnala il puntatore fermo sul
trigger o in uscita, e può mostrarlo su qualsiasi dispositivo.
Appare e scompare con le tue transizioni: startingClass è il suo aspetto nel primo frame e endingClass il suo aspetto
una volta nascosto, che mantiene mentre le classi transition-* del suo class sono in esecuzione; segue onClose.
<Tooltip
class="rounded-md bg-gray-900 px-2 py-1 transition-opacity duration-100"
startingClass="opacity-0"
endingClass="opacity-0"
>
<Text class="text-white">Save the document</Text>
</Tooltip>
#Props
| Prop | Rust | Tipo | Predefinito | Descrizione |
|---|---|---|---|---|
children | .child(..) | JSX.Element | nessuno | Ciò che dice. |
class, style | metodi .app_class(..), Styled | string, StyleProp<ViewStyle> | nessuno | L’aspetto del fumetto: il tooltip stesso. |
startingClass, startingStyle | .starting_class(..), .starting_style(..) | string, StyleProp<ViewStyle> | nessuno | Il suo aspetto nel primo frame: le sue transizioni partono da esso. |
endingClass, endingStyle | .ending_class(..), .ending_style(..) | string, StyleProp<ViewStyle> | nessuno | Il suo aspetto una volta nascosto: lo mantiene mentre le sue transizioni sono in esecuzione. |
open | .open(..) | boolean | nessuno | Visibile, controllato dall’app. |
defaultOpen | .default_open(..) | boolean | false | Visibile all’inizio, senza open. |
delay | .delay(..) | number | 500 | Millisecondi di sosta del puntatore sul trigger prima che venga mostrato; Infinity: mai da solo, solo quando lo apre l’app. |
disabled | .disabled(..) | boolean | false | Non viene mai mostrato. |
placement | .placement(..) | "top" | "bottom" | "left" | "right" | "top" | Il lato del trigger su cui si posiziona. |
align | .align(..) | "start" | "center" | "end" | "center" | Come si allinea al trigger lungo quel lato. |
offset | .offset(..) | number | 4 | Lo spazio tra il trigger e il tooltip. |
margin | .margin(..) | number | 4 | La distanza che mantiene dai bordi della safe area. |
#Eventi
| Evento | Rust | Descrizione |
|---|---|---|
onOpenChange | .on_open_change(|e| ..) | Il puntatore si è fermato sul trigger, oppure lo ha lasciato o premuto (reason "hover"). |
onOpen | .on_open(|| ..) | È mostrato. |
onClose | .on_close(|| ..) | È scomparso, dopo la sua uscita; anche quando l’app lo ha nascosto. |
#Accessibilità
Un tooltip ripete agli screen reader ciò che dice il suo trigger: assegna al trigger anche un accessibilityLabel, così il
suggerimento è disponibile per tutti.