Tailwindcss: 如何同时使用 focus-within 和 focus-visible

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

Tailwindcss: How to focus-within and focus-visible at the same time

问题

只需要翻译代码部分,以下是翻译好的内容:

<div class="flex h-screen items-center justify-center">
  <div class="rounded-lg bg-green-100 px-20 py-20 focus-within:ring-4 focus-within:ring-blue-300">
    <button class="bg-green-200 px-6 py-3 focus:outline-none">可聚焦的按钮</button>
  </div>
</div>

希望这对你有所帮助。如果你有其他问题,可以继续提问。

英文:

I want to focus a div surrounding a button but only when keyboard focused. Usually focus-within works, but in this case it should only focus on keyboard focus (focus-visible:) and not when clicking with the mouse(focus:).

Essentially, I need to combine focus-within and focus-visible. How can this be done?

Tailwind Play: https://play.tailwindcss.com/ApDB5gSjqv

&lt;div class=&quot;flex h-screen items-center justify-center&quot;&gt;
  &lt;div class=&quot;rounded-lg bg-green-100 px-20 py-20 focus-within:ring-4 focus-within:ring-blue-300&quot;&gt;
    &lt;button class=&quot;bg-green-200 px-6 py-3 focus:outline-none&quot;&gt;Focusable Button&lt;/button&gt;
  &lt;/div&gt;
&lt;/div&gt;

Notes:

  • Based on this thread, it looks like w3c doesn't have support for focus-within-visible. What's an alternative or round-about way to achieve this?
  • It looks like there is support for :has(:focus) selector in some browsers but how should this be applied in Tailwind...? Source

答案1

得分: 7

你确实可以使用 :has:focus-visible 来实现你想要的效果。在 caniuse 上检查浏览器支持情况。

使用 arbitrary variants 来构建你的选择器:

<div class="[&amp;:has(:focus-visible)]:ring-4">
  <button>可聚焦的按钮</button>
</div>
英文:

You can indeed use :has with :focus-visible to achieve what you want. Check the browser support on caniuse.

Use arbitrary variants to build your selector:

&lt;div class=&quot;[&amp;:has(:focus-visible)]:ring-4&quot;&gt;
  &lt;button&gt;Focusable button&lt;/button&gt;
&lt;/div&gt;

答案2

得分: -1

Instead of using focus-within you could add relative to the div you want to make look focused with the ring. Then make the button a peer and have an absolutely positioned div after it use the peer-focus-visible modifier to add any "focused" styles you want.

EDIT: You'll need to make the div be behind the button by adding relative to the button and assigning an appropriate z-index value to both the inset div and button.

<div class="flex h-screen items-center justify-center">
  <div class="rounded-lg bg-green-100 px-20 py-20 relative">
    <button class="bg-green-200 px-6 py-3 focus:outline-none peer relative z-[1]">Focusable Button</button>
    <div class="absolute inset-0 rounded-lg peer-focus-visible:ring-4 peer-focus-visible:ring-blue-300 z-[0]"></div>
  </div>
</div>

Here is an edited example on Tailwind Play: Link

英文:

Instead of using focus-within you could add relative to the div you want to make look focused with the ring. Then make the button a peer and have an absolutely positioned div after it use the peer-focus-visible modifier to add any "focused" styles you want.

EDIT: You'll need to make the div be behind the button by adding relative to the button and assigning an appropriate z-index value to both the inset div and button.

&lt;div class=&quot;flex h-screen items-center justify-center&quot;&gt;
  &lt;div class=&quot;rounded-lg bg-green-100 px-20 py-20 relative&quot;&gt;
    &lt;button class=&quot;bg-green-200 px-6 py-3 focus:outline-none peer relative z-[1]&quot;&gt;Focusable Button&lt;/button&gt;
  &lt;div class=&quot;absolute inset-0 rounded-lg peer-focus-visible:ring-4 peer-focus-visible:ring-blue-300 z-[0]&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

</div>

Here is an edited example on Tailwind Play https://play.tailwindcss.com/us5kpJg6cV

huangapple
  • 本文由 发表于 2023年2月7日 03:10:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365592.html
匿名

发表评论

匿名网友

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

确定