元素

键盘

env(keyboard-inset-height) 是软键盘覆盖在界面上的高度,键盘隐藏时为 0。把它用作视图的底部内边距或外边距,可以让内容保持在键盘之上:键盘显示或隐藏时,视图按键盘高度让出空间,变化在原生侧随键盘一起动画。它用于滚动视图之外的内容,例如聊天界面底部的消息栏;滚动视图或 List 中的输入框会在原生侧滚动到可见位置。与它配套的还有隐藏键盘的 Keyboard.dismiss(),以及用于自定义布局的键盘高度。

el-keyboard.tsx
el-keyboard.tsx运行中
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={(next) => enabled.set(next)}
            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 {keyboardFrame().duration.toFixed(2)} s
        </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={(text) => draft.set(text)}
          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(),
                ),
        )
}

完整的键盘高度适用于延伸到屏幕底部的视图,界面的内容通常如此。如果它下方还有东西,例如标签栏,就用一个长度减去它:calc(env(keyboard-inset-height) + -49px) 让视图保持在 49 pt 的标签栏之上,calc(env(keyboard-inset-height) + 12px) 在键盘上方增加间距。内边距至少为 0。

#内边距或外边距

样式让出空间的方式
paddingBottom: "env(keyboard-inset-height)"(Tailwind pb-[env(keyboard-inset-height)],Rust .pb_keyboard())上方的内容缩小;背景仍延伸到屏幕底部。
marginBottom: "env(keyboard-inset-height)"(Tailwind mb-[env(keyboard-inset-height)],Rust .mb_keyboard())整个视图(包括背景)在键盘顶部结束。

键盘高度只与同一声明中的长度相加,因此视图用 calc() 把它和自己的间距组合起来。在 Rust 中,.pb_keyboard_plus(px(..)) 和 .mb_keyboard_plus(px(..)) 加上一个长度。

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))
}

这里视图下方 49 pt 的标签栏已经遮住了一部分键盘高度。要关闭让位,就在样式中去掉这个变量,例如根据一个信号加条件。

#隐藏键盘

nsh 的 Keyboard.dismiss() 让获得焦点的输入框失去焦点,从而隐藏键盘。没有输入框获得焦点时,它不做任何改变。Rust 组件调用 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()))
}

#键盘高度

TSXRust返回
keyboardFrame()keyboard_height()、keyboard_duration()软键盘覆盖在 NSH 界面上的高度,单位为点(隐藏时为 0),以及其显示或隐藏动画的时长,单位为秒。

两者都是响应式的:读取它们的 effect、JSX 表达式或样式闭包会在键盘显示、隐藏或改变高度时重新运行。

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())
}

高度是键盘与 NSH 界面重叠的部分。当窗口本身为键盘缩小时(会调整大小的 Android activity),或 NSH 嵌入在结束于键盘上方的视图中时,高度为 0。

#平台说明

  • iOS:让位以键盘自身的时长和 ease-in-ease-out 曲线动画。
  • Android:窗口为键盘缩小时(会调整大小的 activity),内容已经在键盘上方结束,变量为 0。键盘与视图重叠时,与 iOS 一样让出空间。
  • 桌面平台和 Mac Catalyst 使用硬件键盘输入:变量为 0,视图保持原有布局。
  • Web:变量是浏览器自己的(VirtualKeyboard API 的)。

焦点顺序、回车键和输入附件参见表单与键盘,其他环境变量参见安全区。