Elementos
Teclado
env(keyboard-inset-height) é a altura do teclado virtual sobre a superfície, e vale 0 enquanto ele está oculto. Usada como padding ou margin inferior de uma view, ela mantém o conteúdo acima do teclado: quando o teclado aparece ou some, a view abre espaço com a altura dele, e a mudança é animada junto com o teclado, de forma nativa. Use-a para conteúdo fora de scroll views, como uma barra de mensagem na parte inferior de uma tela de chat; inputs dentro de uma scroll view ou de uma List são roladas até ficarem visíveis de forma nativa. Junto com ela vêm Keyboard.dismiss(), que oculta o teclado, e a altura do teclado, para layouts próprios.
import { mount, Scroll, signal, Text, View } from "nsh";
import { Input } from "nsh/base";
import { Button, Switch } from "nsh/base";
import { Keyboard, keyboardFrame } from "nsh";
import { button, buttonText, desktop, field, tint, ui } from "./palette";
function App() {
const draft = signal("");
const messages = signal(["Type below: the bar stays above the keyboard."]);
const behavior = signal<"padding" | "height">("padding");
const enabled = signal(true);
const send = () => {
if (!draft().trim()) return;
messages.update((list) => [...list, draft().trim()]);
draft.set("");
};
const chip = (on: () => boolean) => button("secondary", { on, size: "sm" });
const chipText = (on: () => boolean) => buttonText("secondary", { on });
const isPadding = () => behavior() === "padding";
const isHeight = () => behavior() === "height";
return (
<View
style={{
flex: 1,
backgroundColor: behavior() === "height" ? tint(ui().info, 0.12) : ui().page,
// Bottom padding (or margin) by the keyboard's height, while enabled.
...(!enabled() ? {} : behavior() === "padding" ? { paddingBottom: "env(keyboard-inset-height)" } : { marginBottom: "env(keyboard-inset-height)" }),
}}
>
<View style={{ padding: 20, paddingTop: 24, paddingBottom: 8, gap: 8 }}>
<Text style={{ fontSize: 22, fontWeight: "700", color: ui().text }}>keyboard inset</Text>
<Text style={{ color: ui().muted }}>
{desktop
? "Desktops type with a hardware keyboard: its height stays 0 and the view keeps its layout. Open this on a phone to see it move."
: "Tap the message field: the bar moves up with the keyboard."}
</Text>
<View style={{ flexDirection: "row", flexWrap: "wrap", gap: 6 }}>
<Button onPress={() => behavior.set("padding")} selected={isPadding()} {...chip(isPadding)}>
<Text style={chipText(isPadding)}>padding</Text>
</Button>
<Button onPress={() => behavior.set("height")} selected={isHeight()} {...chip(isHeight)}>
<Text style={chipText(isHeight)}>height</Text>
</Button>
<Switch
checked={enabled()}
onChange={({ checked }) => enabled.set(checked)}
class="flex-row items-center gap-2 px-1 active:opacity-70"
trackClass="w-9 rounded-full p-0.5 transition-colors motion-reduce:transition-none"
trackStyle={enabled() ? ui().accentFill : ui().inset}
thumbClass="size-4 rounded-full transition-transform motion-reduce:transition-none checked:translate-x-4"
thumbStyle={enabled() ? { backgroundColor: ui().onAccent } : ui().raised}
>
<Text style={{ color: ui().text, fontSize: 13, fontWeight: "600" }}>enabled {String(enabled())}</Text>
</Switch>
</View>
<Text style={{ color: ui().accent, fontSize: 13 }}>
keyboardFrame(): height {Math.round(keyboardFrame().height)} · duration {Math.round(keyboardFrame().duration)} ms
</Text>
</View>
<Scroll style={{ flex: 1 }} keyboardDismissMode="interactive">
<View style={{ padding: 20, paddingTop: 4, gap: 8 }}>
<Each of={messages()}>
{(text) => (
<View style={{ ...ui().row, alignSelf: "flex-start", maxWidth: 320, borderRadius: 14, paddingHorizontal: 12, paddingVertical: 8 }}>
<Text style={{ color: ui().text }}>{text()}</Text>
</View>
)}
</Each>
</View>
</Scroll>
<View style={{ flexDirection: "row", gap: 8, padding: 12, paddingHorizontal: desktop ? 20 : 12, borderTopWidth: 1, borderTopColor: ui().border, backgroundColor: ui().page }}>
<Input
value={draft()}
onChange={({ value }) => draft.set(value)}
onSubmit={send}
testID="message"
returnKeyType="send"
placeholder="Message"
{...field({ style: { flex: 1 } })}
/>
<Button onPress={send} {...button("primary")}>
<Text style={buttonText("primary")}>Send</Text>
</Button>
<Button onPress={() => Keyboard.dismiss()} {...button()}>
<Text style={buttonText()}>Done</Text>
</Button>
</View>
</View>
);
}
mount(App);use nsh_core::prelude::*;
fn chat() -> impl IntoElement {
let draft = signal(String::new());
view()
.flex_1()
.bg(white())
.pb_keyboard()
.child(scroll().flex_1().child(text("Messages…").p_4()))
.child(
view()
.flex_row()
.gap_2()
.p_2()
.border_t_1()
.border_color(rgb(0xe5e5e5))
.child(
input()
.value(draft)
.on_change(move |v: &str| draft.set(v.to_owned()))
.placeholder("Message")
.flex_1()
.p_2(),
),
)
}import { mount, Scroll, signal, Text, View } from "nsh";
import { Input } from "nsh/base";
function App() {
const draft = signal("");
return (
<View style={{ flex: 1, backgroundColor: "#ffffff", paddingBottom: "env(keyboard-inset-height)" }}>
<Scroll style={{ flex: 1 }} keyboardDismissMode="interactive">
<Text style={{ padding: 16, paddingTop: 48 }}>Messages…</Text>
</Scroll>
<View style={{ flexDirection: "row", gap: 8, padding: 8, borderTopWidth: 1, borderTopColor: "#e5e5e5" }}>
<Input
value={draft()}
onChange={({ value }) => draft.set(value)}
placeholder="Message"
style={{ flex: 1, fontSize: 16, padding: 8 }}
/>
</View>
</View>
);
}
mount(App);A altura total do teclado serve para uma view que chega até a parte inferior da tela, como o conteúdo de uma tela. Quando há algo abaixo dela, como uma tab bar, subtraia esse valor com um comprimento: calc(env(keyboard-inset-height) + -49px) mantém uma view acima de uma tab bar de 49 pt, e calc(env(keyboard-inset-height) + 12px) adiciona um espaçamento acima do teclado. Um padding é no mínimo 0.
#Padding ou margin
| Estilo | Como abre espaço |
|---|---|
paddingBottom: "env(keyboard-inset-height)" (Tailwind pb-[env(keyboard-inset-height)], Rust .pb_keyboard()) |
O conteúdo acima encolhe; o fundo continua chegando à parte inferior da tela. |
marginBottom: "env(keyboard-inset-height)" (Tailwind mb-[env(keyboard-inset-height)], Rust .mb_keyboard()) |
A view inteira, incluindo o fundo, termina no topo do teclado. |
A altura do teclado é somada ao comprimento apenas dentro da mesma declaração, de modo que uma view a combina com espaçamento próprio em calc(). Em Rust, .pb_keyboard_plus(px(..)) e .mb_keyboard_plus(px(..)) adicionam um comprimento.
use nsh_core::prelude::*;
fn sign_in() -> impl IntoElement {
view()
.mb_keyboard_plus(px(-49.0))
.flex_1()
.p_4()
.gap_3()
.child(input().placeholder("E-mail").keyboard_type(KeyboardType::Email))
}<View class="flex-1 gap-3 p-4 mb-[calc(env(keyboard-inset-height)+-49px)]">
<Input placeholder="E-mail" keyboardType="email-address" class="rounded-lg border border-neutral-300 px-3 py-2" />
</View>Aqui, uma tab bar de 49 pt abaixo da view já cobre parte da altura do teclado. Para desativar o espaço, deixe a variável fora do estilo, por exemplo com uma condição sobre um signal.
#Ocultando o teclado
Keyboard.dismiss(), de nsh, remove o foco do input focado, o que oculta o teclado. Sem nenhum input focado, ele não altera nada. Componentes Rust chamam dismiss_keyboard().
use nsh_base::button;
use nsh_core::prelude::*;
fn done() -> impl IntoElement {
button()
.on_press(|| dismiss_keyboard())
.rounded_lg()
.bg(rgb(0x2563eb))
.px_4()
.py_3()
.child(text("Done").text_color(white()))
}import { Text, Keyboard } from "nsh";
import { Button } from "nsh/base";
function Done() {
return (
<Button onPress={() => Keyboard.dismiss()} class="rounded-lg bg-blue-600 px-4 py-3 active:bg-blue-700">
<Text class="text-center font-semibold text-white">Done</Text>
</Button>
);
}#A altura do teclado
| TSX | Rust | Retorna |
|---|---|---|
keyboardFrame() |
keyboard_height(), keyboard_duration() |
A altura do teclado virtual sobre a superfície do NSH, em pontos (0 enquanto ele está oculto), e a duração da animação de exibição ou ocultação dele, em milissegundos. |
Ambos são reativos: um effect, uma expressão JSX ou uma closure de estilo que os lê é executado novamente quando o teclado aparece, some ou muda de altura.
use nsh_core::prelude::*;
fn message_bar() -> impl IntoElement {
view()
.flex_row()
.p_2()
.pb(move || {
// The home indicator's inset only matters while the keyboard is hidden.
let home = if keyboard_height() > Points::ZERO {
Points::ZERO
} else {
safe_area().bottom
};
px(8.0) + home
})
.child(input().placeholder("Message").flex_1())
}import { View, keyboardFrame, safeArea } from "nsh";
import { Input } from "nsh/base";
function MessageBar() {
const insets = safeArea();
return (
<View
style={{
flexDirection: "row",
padding: 8,
// The home indicator's inset only matters while the keyboard is hidden.
paddingBottom: 8 + (keyboardFrame().height > 0 ? 0 : insets().bottom),
}}
>
<Input placeholder="Message" style={{ flex: 1 }} />
</View>
);
}A altura é a parte do teclado que se sobrepõe à superfície do NSH. Quando a própria janela encolhe por causa do teclado (uma activity do Android que se redimensiona), ou quando o NSH está embutido em uma view que termina acima do teclado, a altura é 0.
#Notas por plataforma
- iOS: o espaço é animado com a duração própria do teclado e uma curva ease-in-ease-out.
- Android: onde a janela encolhe por causa do teclado (uma activity que se redimensiona), o conteúdo já termina acima do teclado e a variável vale 0. Onde o teclado se sobrepõe à view, ela abre espaço como no iOS.
- Desktops e Mac Catalyst usam teclado físico para digitar: a variável vale 0 e a view mantém o layout.
- Web: a variável é a do próprio navegador (a da VirtualKeyboard API).
Veja Formulários e teclado para ordem de foco, teclas de retorno e acessórios de input, e Safe area para as demais variáveis de ambiente.