跳到主要内容

useTextDirection

跟踪文字排列方向

Usage

实时编辑器

function Demo() {
  const [dir, setDir] = useTextDirection({
    selector: "#_useTextDirectionDemo",
  });

  const text = useMemo(
    () =>
      dir === "ltr"
        ? "This paragraph is in English and correctly goes left to right."
        : "This paragraph is in English but incorrectly goes right to left.",
    [dir],
  );

  const handleOnClick = () => {
    const value = dir === "rtl" ? "ltr" : "rtl";
    setDir(value);
  };

  return (
    <div id="_useTextDirectionDemo">
      <p>{text}</p>
      <button onClick={handleOnClick}>
        <span>{dir?.toUpperCase()}</span>
      </button>
      <span>Click to change the direction</span>
    </div>
  );
};

render(<Demo/>)
结果
Loading...

API

useTextDirection

Returns

readonly [UseTextDirectionValue, (value: UseTextDirectionValue) => void]: 包含以下元素的元组:

  • 文字方向。
  • 更新文字方向值的函数。

Arguments

参数名描述类型默认值
options可选参数UseTextDirectionOptions | undefined-

UseTextDirectionOptions

参数名描述类型默认值
selector适用于目标元素的 CSS 选择器string'html'
initialValue初始值UseTextDirectionValue'ltr'

UseTextDirectionValue

Type

export type UseTextDirectionValue = "ltr" | "rtl" | "auto";