• Stars
    star
    2,106
  • Rank 21,914 (Top 0.5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Utilities and variants for styling Radix state

Utilities and variants for styling Radix state

tailwindcss v3 ready npm version npm downloads

Installation

npm i tailwindcss-radix
yarn add tailwindcss-radix

Migrate from v1

To prevent a possible future name clashing the skipAttributeNames option has been removed. In case you used this option, please update the class names accordingly.

Demo

Click on the banner to check out the demo components. You can find the code inside the demo folder.

Usage

Add the plugin to your plugins array:

module.exports = {
  theme: {
    // --snip--
  },
  variants: {
    // --snip--
  },
  plugins: [
    // Initialize with default values (see options below)
    require("tailwindcss-radix")(),
  ],
};

Options

require("tailwindcss-radix")({
  // Default: `radix`
  variantPrefix: "rdx",
});
// Example 1: Generates `rdx-[state/side/orientation]-*` utilities for `data-[state/side/orientation]="*"`
variantPrefix: "rdx",

// Example 2: Generates `[state/side/orientation]-*` utilities for `data-[state/side/orientation]="*"`
variantPrefix: false,

Styling state

Basic usage

This plugin works with CSS attribute selectors. Use the variants based on the data-* attribute added by Radix.

import React from "react";
import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu";

const App = () => {
  return (
    <DropdownMenuPrimitive.Root>
      <DropdownMenuPrimitive.Trigger className="border-black radix-state-open:border-2">
        Trigger
      </DropdownMenuPrimitive.Trigger>
      <DropdownMenuPrimitive.Content>
        <DropdownMenuPrimitive.Item>Item</DropdownMenuPrimitive.Item>
      </DropdownMenuPrimitive.Content>
    </DropdownMenuPrimitive.Root>
  );
};

export default App;

Accessing parent state

When you need to style an element based on the state of a parent element, mark the parent with the group class and style the target with group-radix-* modifiers.

Example usage of a conditional transform for a Radix Accordion:

import React from "react";
import * as AccordionPrimitive from "@radix-ui/react-accordion";
import { ChevronDownIcon } from "@radix-ui/react-icons";

const Accordion = () => {
  return (
    <AccordionPrimitive.Root type="multiple">
      <AccordionPrimitive.Item value="item-1">
        <AccordionPrimitive.Header>
          <AccordionPrimitive.Trigger className="group">
            <div className="flex items-center">
              Item 1
              <ChevronDownIcon className="w-5 h-5 ml-2 transform group-radix-state-open:rotate-180" />
            </div>
          </AccordionPrimitive.Trigger>
        </AccordionPrimitive.Header>
        <AccordionPrimitive.Content>Content 1</AccordionPrimitive.Content>
      </AccordionPrimitive.Item>
      <AccordionPrimitive.Item value="item-2">
        <AccordionPrimitive.Header>
          <AccordionPrimitive.Trigger className="group">
            <div className="flex items-center">
              Item 2
              <ChevronDownIcon className="w-5 h-5 ml-2 transform group-radix-state-open:rotate-180" />
            </div>
          </AccordionPrimitive.Trigger>
        </AccordionPrimitive.Header>
        <AccordionPrimitive.Content>Content 2</AccordionPrimitive.Content>
      </AccordionPrimitive.Item>
    </AccordionPrimitive.Root>
  );
};

export default App;

Accessing sibling state

When you need to style an element based on the state of a sibling element, mark the sibling with the peer class and style the target with peer-radix-* modifiers.

Example usage of a conditional icon color for a sibling of a Radix Checkbox:

import * as CheckboxPrimitive from "@radix-ui/react-checkbox";
import { CheckIcon, TargetIcon } from "@radix-ui/react-icons";
import React from "react";

interface Props {}

const App = (props: Props) => {
  return (
    <>
      <CheckboxPrimitive.Root id="c1" defaultChecked className="peer h-5 w-5">
        <CheckboxPrimitive.Indicator>
          <CheckIcon />
        </CheckboxPrimitive.Indicator>
      </CheckboxPrimitive.Root>

      <TargetIcon className="text-red-500 peer-radix-state-checked:text-green-500" />
    </>
  );
};

export default App;

Disabled state

Use the generated disabled variant.

import React from "react";
import * as ContextMenuPrimitive from "@radix-ui/react-context-menu";

const ContextMenu = () => {
  return (
    // --snip--
    <ContextMenuPrimitive.Item
      disabled
      className="radix-disabled:opacity-50 radix-disabled:cursor-not-allowed"
    >
      Item
    </ContextMenuPrimitive.Item>
    // --snip--
  );
};

Animation

Origin position

Use the generated origin-* utilities to transform from the content position origin.

.origin-radix-context-menu {
  transform-origin: var(--radix-context-menu-content-transform-origin);
}

.origin-radix-dropdown-menu {
  transform-origin: var(--radix-dropdown-menu-content-transform-origin);
}

.origin-radix-hover-card {
  transform-origin: var(--radix-hover-card-content-transform-origin);
}

.origin-radix-menubar {
  transform-origin: var(--radix-menubar-content-transform-origin);
}

.origin-radix-popover {
  transform-origin: var(--radix-popover-content-transform-origin);
}

.origin-radix-select {
  transform-origin: var(--radix-select-content-transform-origin);
}

.origin-radix-tooltip {
  transform-origin: var(--radix-tooltip-content-transform-origin);
}

Content size

Use the generated h-* and w-* utilities to animate the size of the content when it opens/closes.

.h-radix-accordion {
  height: var(--radix-accordion-content-height);
}

.w-radix-accordion {
  width: var(--radix-accordion-content-width);
}

.h-radix-collapsible {
  height: var(--radix-collapsible-content-height);
}

.w-radix-collapsible {
  width: var(--radix-collapsible-content-width);
}

Toast swiping

Use the generated translate-* utilities to animate swipe gestures.

.translate-x-radix-toast-swipe-move-x {
  transform: translateX(var(--radix-toast-swipe-move-x));
}

.translate-y-radix-toast-swipe-move-y {
  transform: translateY(var(--radix-toast-swipe-move-y));
}

.translate-x-radix-toast-swipe-end-x {
  transform: translateX(var(--radix-toast-swipe-end-x));
}

.translate-y-radix-toast-swipe-end-y {
  transform: translateY(var(--radix-toast-swipe-end-y));
}

License

MIT

More Repositories

1

tailwindcss-figma-kit

Figma Kit for Tailwind CSS
Shell
736
star
2

tailwindcss-figma-plugin

Figma Plugin for Tailwind CSS
TypeScript
269
star
3

react-hooks-mobx-state-tree

React Hooks + MobX State Tree + TypeScript = 💛
TypeScript
242
star
4

flutters

A game powered by Flutter and Flame
Dart
240
star
5

react-native-heroicons

React Native components for heroicons
TypeScript
136
star
6

ac2100-openwrt-guide

Install OpenWrt on the AC2100 (black cylinder)
Python
113
star
7

gatsby-typescript-tailwind

Gatsby + TypeScript + Tailwind CSS
TypeScript
76
star
8

webpack4-pug-sass-example

Example config for using pug and sass in webpack4
JavaScript
49
star
9

heroicons-react

React components for heroicons
TypeScript
39
star
10

OpenCore-Lenovo-Flex-15

macOS support for the Lenovo Flex 15 59416277
Shell
23
star
11

coingecko-rs

Rust library for the CoinGecko V3 API
Rust
17
star
12

OpenCore-NUC8i5BEH

OpenCore EFI for the Intel NUC8i5BEH
14
star
13

cra-tailwind-starter

Tailwind CSS starter project based on create-react-app
JavaScript
13
star
14

react-popper-spring-tooltip

A highly configurable tooltip wrapper around react-popper and react-spring
TypeScript
12
star
15

dotfiles

dotfiles - macOS
Lua
11
star
16

OpenCore-EliteDesk-800-G2SFF

OpenCore EFI for the HP EliteDesk 800 G2 SFF
10
star
17

firebase-functions-boilerplate

Template for having split up cloud functions with jest testing
TypeScript
8
star
18

react-native-next-monorepo

https://ecklf.com/blog/rn-monorepo
Java
6
star
19

tailwind.json

npx tailwind.json
JavaScript
6
star
20

react-tailwind-storybook

Create a TailwindCSS component library with Storybook
JavaScript
3
star
21

rustlings_solutions

My rustlings solutions
Rust
3
star
22

react-native-animation-utils

TypeScript
2
star
23

flagkit

TypeScript
1
star
24

snake-rs

Rust
1
star
25

rust_learning

Rust
1
star
26

tailwindcss-bot

Tailwind CSS Discord Helper Bot
Rust
1
star
27

cra-tailwind-ts

Tailwind CSS starter project based on create-react-app (TypeScript)
TypeScript
1
star
28

advent-of-code-2021

Advent of Code 2021
Rust
1
star
29

react-navigation-v6-monorepo-issue

Java
1
star
30

mst-gql-compile-bug

https://github.com/mobxjs/mst-gql/issues/252
TypeScript
1
star
31

react-css.gg

TypeScript
1
star
32

react-edge-functions

TypeScript
1
star
33

nextjs-tailwind-ts

TypeScript
1
star
34

advent-of-code-2022

Rust
1
star
35

ecklf

1
star
36

OpenCore-Lenovo-M720q

Lenovo M720q - OpenCore EFI for macOS 15 Sequoia
1
star
37

macos-tags

Rust library for modifying macOS tags
Rust
1
star
38

react-flat-flags

Flat Flags for React
JavaScript
1
star