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

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

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

问题

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

这是问题的一个小示例:

  1. function ExampleInput({
  2. label,
  3. className,
  4. onClear,
  5. ...props
  6. }: { label: string; onClear: () => void } & ComponentProps<'input'>) {
  7. const id = useId();
  8. return (
  9. <div>
  10. <label htmlFor={id}>
  11. <span>{label}</span>
  12. <input
  13. {...props}
  14. className={clsx('peer truncate text-lg/6', className)}
  15. type="text"
  16. id={id}
  17. />
  18. <button
  19. className="not(:peer-placeholder-shown):peer-focus-within:block absolute bottom-0.5 right-0.5 hidden text-spanishGrey"
  20. type="button"
  21. >
  22. <CloseCrossIcon onClick={onClear} height="14" width="14" />
  23. </button>
  24. </label>
  25. </div>
  26. );
  27. }

目标是只有在占位符隐藏且输入聚焦时才显示按钮,而且只使用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:

  1. function ExampleInput({
  2. label,
  3. className,
  4. onClear,
  5. ...props
  6. }: { label: string; onClear: () =&gt; void } &amp; ComponentProps&lt;&#39;input&#39;&gt;) {
  7. const id = useId();
  8. return (
  9. &lt;div&gt;
  10. &lt;label htmlFor={id}&gt;
  11. &lt;span&gt;{label}&lt;/span&gt;
  12. &lt;input
  13. {...props}
  14. className={clsx(&#39;peer truncate text-lg/6&#39;, className)}
  15. type=&quot;text&quot;
  16. id={id}
  17. /&gt;
  18. &lt;button
  19. className=&quot;not(:peer-placeholder-shown):peer-focus-within:block absolute bottom-0.5 right-0.5 hidden text-spanishGrey&quot;
  20. type=&quot;button&quot;
  21. &gt;
  22. &lt;CloseCrossIcon onClick={onClear} height=&quot;14&quot; width=&quot;14&quot; /&gt;
  23. &lt;/button&gt;
  24. &lt;/label&gt;
  25. &lt;/div&gt;
  26. );
  27. }

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

  1. &lt;script src=&quot;https://cdn.tailwindcss.com/3.3.3&quot;&gt;&lt;/script&gt;
  2. &lt;label&gt;
  3. &lt;span&gt;{label}&lt;/span&gt;
  4. &lt;input class=&quot;peer&quot; type=&quot;text&quot; placeholder=&quot;foo&quot; /&gt;
  5. &lt;button class=&quot;hidden peer-[:not(:placeholder-shown):focus]:block&quot;&gt;
  6. CloseCrossIcon
  7. &lt;/button&gt;
  8. &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:

确定