Tailwindcss 将 `peer-focus-within` 与 `not(:peer-placeholder-shown)` 结合使用。

huangapple go评论66阅读模式
英文:

Tailwindcss combine peer-focus-within with not(:peer-placeholder-shown)

问题

我想创建一个带有清除图标的输入字段,只有在输入焦点且占位符不可见时才显示。在项目中,我们在React中使用最新版本的tailwindcss。
我尝试使用not(:)运算符,但这不起作用。

这是问题的一个小示例:

function ExampleInput({
  label,
  className,
  onClear,
  ...props
}: { label: string; onClear: () => void } & ComponentProps<'input'>) {
  const id = useId();

  return (
    <div>
      <label htmlFor={id}>
        <span>{label}</span>
        <input
          {...props}
          className={clsx('peer truncate text-lg/6', className)}
          type="text"
          id={id}
        />

        <button
          className="not(:peer-placeholder-shown):peer-focus-within:block absolute bottom-0.5 right-0.5 hidden text-spanishGrey"
          type="button"
        >
          <CloseCrossIcon onClick={onClear} height="14" width="14" />
        </button>
      </label>
    </div>
  );
}

目标是只有在占位符隐藏且输入聚焦时才显示按钮,而且只使用CSS,也许可以使用自定义类或扩展tailwind主题。

英文:

I want to create a input field with an clear icon that only shows when the input is focused and the placeholder is not shown. In the project we use tailwindcss latest version in react.
I tried using the not(:) operator but this does not work.

Here is a small example of the problem:

function ExampleInput({
  label,
  className,
  onClear,
  ...props
}: { label: string; onClear: () =&gt; void } &amp; ComponentProps&lt;&#39;input&#39;&gt;) {
  const id = useId();

  return (
    &lt;div&gt;
      &lt;label htmlFor={id}&gt;
        &lt;span&gt;{label}&lt;/span&gt;
        &lt;input
          {...props}
          className={clsx(&#39;peer truncate text-lg/6&#39;, className)}
          type=&quot;text&quot;
          id={id}
        /&gt;

        &lt;button
          className=&quot;not(:peer-placeholder-shown):peer-focus-within:block absolute bottom-0.5 right-0.5 hidden text-spanishGrey&quot;
          type=&quot;button&quot;
        &gt;
          &lt;CloseCrossIcon onClick={onClear} height=&quot;14&quot; width=&quot;14&quot; /&gt;
        &lt;/button&gt;
      &lt;/label&gt;
    &lt;/div&gt;
  );
}

The goal would be that the button is only shown when the placeholder is hidden and the input is focused with only css. Maybe with a custom class or extension of the tailwind theme.

答案1

得分: 1

你可以考虑使用任意对等变体peer-[:not(:placeholder-shown):focus]。还请注意,&lt;input&gt; 需要具有非空的 placeholder 属性值,以便匹配 :placeholder-shown

英文:

You could consider using an arbitrary peer variant like peer-[:not(:placeholder-shown):focus]. Also note that the &lt;input&gt; needs a non-empty placeholder attribute value for :placeholder-shown to match.

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.tailwindcss.com/3.3.3&quot;&gt;&lt;/script&gt;

&lt;label&gt;
  &lt;span&gt;{label}&lt;/span&gt;
  &lt;input class=&quot;peer&quot; type=&quot;text&quot; placeholder=&quot;foo&quot; /&gt;
  &lt;button class=&quot;hidden peer-[:not(:placeholder-shown):focus]:block&quot;&gt;
    CloseCrossIcon
  &lt;/button&gt;
&lt;/label&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月24日 15:54:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定