How do I extend the peer- modifier in Tailwind to allow it to work on any sibling?

I would like to extend the peer- modifier in Tailwind such that when I write this:

<div class="peer peer-hover:opacity-50"></div>
<div class="peer peer-hover:opacity-50"></div>
<div class="peer peer-hover:opacity-50"></div>

Then when I hover over any of those divs, the divs I’m not hovered over have a lowered opacity. By default, it only applies the opacity change to the siblings after the hovered element, but not the siblings before it. This can be fixed by adding the class has-[~_.peer:hover]:opacity-50 to all of those divs, which applies the opacity change to the siblings before the hovered element.

I would like to have the peer-mod:style modifer automatically add the matching has-[~_.peer:mod]:style, or create a new modifier that adds both, so that I don’t need to write both every time I need to use it. I also want it to work with any modifier, not just hover. Is there a way I can write a custom plugin to make this work?

Here’s a demo to show what I’d like to achieve: https://play.tailwindcss.com/tKt6L7S447

<div class="absolute inset-0 flex flex-col items-center justify-center gap-10">
  <div class="flex flex-col items-center gap-4">
    <p class="text-xl">Default Peer Hover (Can only style subsequent siblings)</p>
    <div class="flex items-center justify-center gap-12">
      <div class="peer size-24 rounded-md bg-lime-400 transition-transform hover:scale-125 peer-hover:scale-50 peer-hover:opacity-50"></div>
      <div class="peer size-24 rounded-md bg-lime-400 transition-transform hover:scale-125 peer-hover:scale-50 peer-hover:opacity-50"></div>
      <div class="peer size-24 rounded-md bg-lime-400 transition-transform hover:scale-125 peer-hover:scale-50 peer-hover:opacity-50"></div>
      <div class="peer size-24 rounded-md bg-lime-400 transition-transform hover:scale-125 peer-hover:scale-50 peer-hover:opacity-50"></div>
    </div>
  </div>

  <div class="flex flex-col items-center gap-4">
    <p class="text-xl">Better Peer Hover (Styles any sibling)</p>
    <div class="flex items-center justify-center gap-12">
      <div class="peer size-24 rounded-md bg-rose-300 transition-transform hover:scale-125 peer-hover:scale-50 has-[~_.peer:hover]:scale-50 peer-hover:opacity-50 has-[~_.peer:hover]:opacity-50"></div>
      <div class="peer size-24 rounded-md bg-rose-300 transition-transform hover:scale-125 peer-hover:scale-50 has-[~_.peer:hover]:scale-50 peer-hover:opacity-50 has-[~_.peer:hover]:opacity-50"></div>
      <div class="peer size-24 rounded-md bg-rose-300 transition-transform hover:scale-125 peer-hover:scale-50 has-[~_.peer:hover]:scale-50 peer-hover:opacity-50 has-[~_.peer:hover]:opacity-50"></div>
      <div class="peer size-24 rounded-md bg-rose-300 transition-transform hover:scale-125 peer-hover:scale-50 has-[~_.peer:hover]:scale-50 peer-hover:opacity-50 has-[~_.peer:hover]:opacity-50"></div>
    </div>
  </div>
</div>

1

You can override core’s peer-* variants with your own. Return an array value in the matchVariants() callback to add multiple selectors at once:

require('tailwindcss/plugin')(({ matchVariant }) => {
  matchVariant(
    'peer',
    (value) => [
      `.peer${value} ~ &`,
      `&:has(~ .peer${value})`
    ],
    {
      values: {
        'hover': ':hover',
      }
    }
  );
}),

tailwind.config = {
  plugins: [
    tailwind.plugin(({ matchVariant }) => {
      matchVariant(
        'peer',
        (value) => [
          `.peer${value} ~ &`,
          `&:has(~ .peer${value})`
        ],
        {
          values: {
            'hover': ':hover',
          }
        }
      );
    }),
  ],
}
<script src="https://cdn.tailwindcss.com/3.4.5"></script>

<div class="absolute inset-0 flex flex-col items-center justify-center gap-10">
  <div class="flex flex-col items-center gap-4">
    <p class="text-xl">Default Peer Hover (Can only style subsequent siblings)</p>
    <div class="flex items-center justify-center gap-12">
      <div class="peer size-24 rounded-md bg-lime-400 transition-transform hover:scale-125 peer-hover:scale-50 peer-hover:opacity-50"></div>
      <div class="peer size-24 rounded-md bg-lime-400 transition-transform hover:scale-125 peer-hover:scale-50 peer-hover:opacity-50"></div>
      <div class="peer size-24 rounded-md bg-lime-400 transition-transform hover:scale-125 peer-hover:scale-50 peer-hover:opacity-50"></div>
      <div class="peer size-24 rounded-md bg-lime-400 transition-transform hover:scale-125 peer-hover:scale-50 peer-hover:opacity-50"></div>
    </div>
  </div>
</div>

This is a simplified version. In reality, you may wish to fully adapt Tailwind’s peer plugin code, but this is more complex.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật