https://github.com/brandonmcconnell/tailwindcss-signals
Skip to content
Navigation Menu
Toggle navigation
Sign in
* Product
+
Actions
Automate any workflow
+
Packages
Host and manage packages
+
Security
Find and fix vulnerabilities
+
Codespaces
Instant dev environments
+
GitHub Copilot
Write better code with AI
+
Code review
Manage code changes
+
Issues
Plan and track work
+
Discussions
Collaborate outside of code
Explore
+ All features
+ Documentation
+ GitHub Skills
+ Blog
* Solutions
By size
+ Enterprise
+ Teams
+ Startups
By industry
+ Healthcare
+ Financial services
+ Manufacturing
By use case
+ CI/CD & Automation
+ DevOps
+ DevSecOps
* Resources
Topics
+ AI
+ DevOps
+ Security
+ Software Development
+ View all
Explore
+ Learning Pathways
+ White papers, Ebooks, Webinars
+ Customer Stories
+ Partners
* Open Source
+
GitHub Sponsors
Fund open source developers
+
The ReadME Project
GitHub community articles
Repositories
+ Topics
+ Trending
+ Collections
* Enterprise
+
Enterprise platform
AI-powered developer platform
Available add-ons
+
Advanced Security
Enterprise-grade security features
+
GitHub Copilot
Enterprise-grade AI features
+
Premium Support
Enterprise-grade 24/7 support
* Pricing
Search or jump to...
Search code, repositories, users, issues, pull requests...
Search
[ ]
Clear
Search syntax tips
Provide feedback
We read every piece of feedback, and take your input very seriously.
[ ] [ ] Include my email address so I can be
contacted
Cancel Submit feedback
Saved searches
Use saved searches to filter your results more quickly
Name [ ]
Query [ ]
To see all available qualifiers, see our documentation.
Cancel Create saved search
Sign in
Sign up Reseting focus
You signed in with another tab or window. Reload to refresh your
session. You signed out in another tab or window. Reload to refresh
your session. You switched accounts on another tab or window. Reload
to refresh your session. Dismiss alert
{{ message }}
brandonmcconnell / tailwindcss-signals Public
* Notifications You must be signed in to change notification
settings
* Fork 10
* Star 745
Signals for Tailwind CSS simplifies styling based on ancestor state
via style queries. Its declarative API for signaling states
eliminates complex selectors, resulting in cleaner, more maintainable
code.
License
MIT license
745 stars 10 forks Branches Tags Activity
Star
Notifications You must be signed in to change notification settings
* Code
* Issues 2
* Pull requests 1
* Actions
* Projects 0
* Security
* Insights
Additional navigation options
* Code
* Issues
* Pull requests
* Actions
* Projects
* Security
* Insights
brandonmcconnell/tailwindcss-signals
This commit does not belong to any branch on this repository, and may
belong to a fork outside of the repository.
main
BranchesTags
Go to file
Code
Folders and files
Name Name Last commit Last commit
message date
Latest commit
History
49 Commits
dist dist
.eslintrc.json .eslintrc.json
.gitignore .gitignore
.prettierignore .prettierignore
.prettierrc .prettierrc
LICENSE LICENSE
README.md README.md
index.ts index.ts
package-lock.json package-lock.json
package.json package.json
tsconfig.json tsconfig.json
View all files
Repository files navigation
* README
* MIT license
Signals for Tailwind CSS
minified size license version twitter
[?][?] This plugin is experimental and relies on style queries (via
container queries), which are not yet widely supported in browsers.
The good news is that Safari and Firefox, the browsers lacking
support, have already begun implementing style queries in their
development versions, so it's only a matter of time before they're
widely available.
See the browser compatibility table on MDN or caniuse for more
information.
Signals for Tailwind CSS is a plugin that utilizes style queries (via
container queries) to reactively enable a custom state, which can
then be consumed by any of its descendants in the DOM.
signal is similar to the existing group variant/utility in that both
provide methods for styling elements based on their ancestors' state.
Unlike group states, however, signal states can be explicitly
signaled, allowing their state to be both set and consumed with a
single, simple, unchained variant.
This reduces development effort and the need to compose a chain of
variants, improving the developer experience with a more declarative
API.
Depending on your use case, a traditional group may make more sense,
but often, particularly when managing a parent or ancestor state with
anything more complex than a simple peer-X or group-X, a signal may
be a simpler option.
Installation
You can install the plugin via npm:
npm install tailwindcss-signals
Then, include it in your tailwind.config.js:
module.exports = {
plugins: [
require('tailwindcss-signals'),
]
}
Usage
The plugin introduces the signal variant, which can be used to apply
styles based on an ancestor's signaled state.
Here's an example comparing the traditional approach with the new
signals approach:
Example: Without Signals
check/uncheck here
or hover here
Open this example in Tailwind Play: https://play.tailwindcss.com/
E3ig9SPTsc
Example: With Signals
check/uncheck here
or hover here
Open this example in Tailwind Play: https://play.tailwindcss.com/
weFkMf4U5K
Notice how, with signals, we don't have to use any arbitrary selector
variants like [&>div] and can instead apply those styles directly to
the targeted descendants. This allows us to consolidate some
redundancy in the parent so that whatever condition activates the
signal only needs to be specified once rather than once per style/
utility.
The benefits of Signals for Tailwind CSS become more apparent as the
complexity of your styles and conditions increase.
Activating a signal based on a descendant condition
The general purpose of this plugin is to provide a declarative
approach to applying styles based on an ancestor's state.
However, thanks to the power of the :has() CSS pseudo-class, we can
even activate a signal based on a descendant's state.
Example: Descendant condition
check/uncheck here
or hover here
Open this example in Tailwind Play: https://play.tailwindcss.com/
YnlzSITNqF
This is most useful for situations where you want to apply styles to
an entire block based on the current state of one of its descendants.
Here are a few examples of cases where such a feature might be
useful:
* Activating a signal based on the presence or visibility of a
specific child element
* Activating a signal on a form based on the validity of one or
more of its descendant form fields
* Activating a signal when a specific descendant element is focused
or hovered
* Activating a signal based on the presence of a specific class on
a descendant element
* and many more!
[?][?] Some cautions:
* Watch out for circularity issues. If you set up a signal that
activates based on a descendant's state, and that descendant's
state is also based on the signal, you may run into issues.
* In some cases, if you want to check if any descendant is focused,
for example, you may not need :has() and could use a simpler
pseudo-class variant such as...
+ focus-within:signal instead of has-[:focus]:signal
+ valid:signal instead of has-[:valid]:signal (for a form,
which checks if all form contents are valid)
* This is a bit less declarative when you use :has(), but for use
cases where you would need it, it would likely still be simpler
than the alternative.
Differentiating signals
When using multiple signals, you may run into situations where you
want one signal nested in another, which could cause issues. In that
case, you can distinguish signals apart by naming them using the
modifier syntax built into Tailwind CSS, the same naming convention
used for group and peer variants.
Example: Naming a signal
check/uncheck here
hover/unhover here
press me
Open this example in Tailwind Play: https://play.tailwindcss.com/
MkWvEuaWtO
By giving a signal a name, you can ensure it is unique and doesn't
conflict with other signals. You can name a signal by adding a slash
and the name after the signal variant, like signal/{name}.
Consuming a named signal is the same as consuming a regular signal,
but with the name appended to the variant: signal/{name}.
For more information on this modifier syntax, see Differentiating
peers from the official Tailwind CS documentation.
Why use Signals for Tailwind CSS?
Signals for Tailwind CSS provides a more declarative and
straightforward approach to applying styles based on an ancestor's
state. Leveraging style queries (via container queries) eliminates
the need for complex selector chaining and arbitrary targeting,
resulting in a cleaner and more maintainable codebase.
This plugin is particularly useful for:
* Simplifying the application of styles based on ancestor states
* Improving developer experience with a more declarative API
* Reducing the need for complex selector chaining and arbitrary
targeting
Why NOT use Signals for Tailwind CSS?
[?][?] Browser support for style queries is still limited, so Signals for
Tailwind CSS may not be suitable for projects that require broad
compatibility.
The good news is that Safari and Firefox, the browsers lacking
support, have already begun implementing style queries in their
development versions, so it's only a matter of time before they're
widely available.
See the browser compatibility table on MDN or caniuse for more
information.
---------------------------------------------------------------------
I hope you find tailwindcss-signals a valuable addition to your
projects. If you have any issues or suggestions, don't hesitate to
open an issue or pull request.
If you liked this, you might also like my other Tailwind CSS plugins:
* tailwindcss-multi: Group utilities together by variant
* tailwindcss-mixins: Construct reusable & aliased sets of
utilities inline
* tailwindcss-members: Apply styles based on child or descendant
state, the inverse of groups
* tailwindcss-selector-patterns: Dynamic CSS selector patterns
* tailwindcss-js: Effortless build-time JS script injection
* tailwindcss-directional-shadows: Supercharge your shadow
utilities with added directional support (includes directional
shadow-border utilities too )
* tailwindcss-default-shades: Default shades for simpler color
utility classes
* tailwind-lerp-colors: Expand your color horizons and take the
fuss out of generating new--or expanding existing--color palettes
About
Signals for Tailwind CSS simplifies styling based on ancestor state
via style queries. Its declarative API for signaling states
eliminates complex selectors, resulting in cleaner, more maintainable
code.
Topics
tailwind tailwindcss tailwind-css tailwindcss-plugin
Resources
Readme
License
MIT license
Activity
Stars
745 stars
Watchers
5 watching
Forks
10 forks
Report repository
Releases
23 tags
Packages 0
No packages published
Languages
* TypeScript 100.0%
Footer
(c) 2024 GitHub, Inc.
Footer navigation
* Terms
* Privacy
* Security
* Status
* Docs
* Contact
* Manage cookies
* Do not share my personal information
You can't perform that action at this time.