1.0.6 • Published 1 month ago

tailwindcss-3d v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

3D Plugin for Tailwind CSS

NPM package version Lint with ESLint Tests with Jest Test Coverage on Codecov

Add 3D transforms to your TailwindCSS project.

Description

3D Plugin for Tailwind CSS adds additional transform utilities and animations which can help you to add three dimensional styling to your interface.

By default TailwindCSS adds transforms in two dimensions ("x" and "y" axis only), so scale, rotate, and translate are flat transformations. This plugin adds support for the "z" axis and implements extensions to the bounce and spin animations to allow them to operate in multiple directions.

Tailwind's core implementation uses the transform CSS property to achieve all transformations. This method is better supported by older browsers, but has some limitations which make it more difficult to use.

This plugin implements transformation using newer CSS properties like rotate, translate, and scale where support in more recent browsers allows. This opens some new possibilities when combining utility classes with animations, and in the composition of new animations.

Browser compatibility

Since this pluign uses newer CSS properties, it will produce CSS which is not compatible with some older browsers.

Refer to this list of CSS features on CanIUse.com

If you want to support older browsers, you can use legacy mode.

Installation

Install the plugin using npm or your preferred package manager:

# npm
npm install -D tailwindcss-3d

# yarn
yarn add -D tailwindcss-3d

# pnpm
pnpm add -D tailwindcss-3d

Then add the plugin to your tailwind.config.js file:

// tailwind.config.js
module.exports = {
  theme: {
    // ...
  },
  plugins: [
    require('tailwindcss-3d'),
    // ...
  ],
}

Turn on legacy mode to support older browsers like this:

// tailwind.config.js
module.exports = {
  theme: {
    // ...
  },
  plugins: [
    require('tailwindcss-3d')({ legacy: true }),
    // ...
  ],
}

All configuration of utility values is done via theme configuration in your tailwind.config.js file.

Appropriate theme configuration points are indicated under each utility below.

Here is an example of extending the available rotation values to add a 30 degree and a 60 degree rotation value on all axes:

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      rotate: {
        '30': '30deg',
        '60': '60deg',
      },
    },
    // ...
  },
  plugins: [
    require('tailwindcss-3d'),
    // ...
  ],
}

For more information about extending your theme, see the relevant Tailwind documentation.

Usage

A note on coordinates

"y" axis values in the CSS coordinate system can be a bit confusing at first. The origin of the coordinate system, where x = 0, y = 0 in two-dimensional space is the top-left of the browser window. From there, positive values of "x" are to the right as expected, but positive values of "y" are in the downward direction from that point. This is the opposite of common usage in other contexts like maths and construction.

The effect of this is felt most when translating elements which are in the middle of a page, and also in the case of this plugin when applying directions to animations.

Utilities

scale

Tailwind Play scale utility examples

Class.Properties
scale-x-0scale: 0 … …
scale-y-0scale: … 0 …
scale-z-0scale: … … 0
scale-0scale: 0 0 …
scale3d-0scale: 0 0 0
scale-x-50scale: .5 … …
scale-y-50scale: … .5 …
scale-z-50scale: … … .5
scale-50scale: .5 .5 …
scale3d-50scale: .5 .5 .5
scale-x-75scale: .75 … …
scale-y-75scale: … .75 …
scale-z-75scale: … … .75
scale-75scale: .75 .75 …
scale3d-75scale: .75 .75 .75
scale-x-90scale: .9 … …
scale-y-90scale: … .9 …
scale-z-90scale: … … .9
scale-90scale: .9 .9 …
scale3d-90scale: .9 .9 .9
scale-x-95scale: .95 … …
scale-y-95scale: … .95 …
scale-z-95scale: … … .95
scale-95scale: .95 .95 …
scale3d-95scale: .95 .95 .95
scale-x-100scale: 1 … …
scale-y-100scale: … 1 …
scale-z-100scale: … … 1
scale-100scale: 1 1 …
scale3d-100scale: 1 1 1
scale-x-105scale: 1.05 … …
scale-y-105scale: … 1.05 …
scale-z-105scale: … … 1.05
scale-105scale: 1.05 1.05 …
scale3d-105scale: 1.05 1.05 1.05
scale-x-110scale: 1.1 … …
scale-y-110scale: … 1.1 …
scale-z-110scale: … … 1.1
scale-110scale: 1.1 1.1 …
scale3d-110scale: 1.1 1.1 1.1
scale-x-125scale: 1.25 … …
scale-y-125scale: … 1.25 …
scale-z-125scale: … … 1.25
scale-125scale: 1.25 1.25 …
scale3d-125scale: 1.25 1.25 1.25
scale-x-150scale: 1.5 … …
scale-y-150scale: … 1.5 …
scale-z-150scale: … … 1.5
scale-150scale: 1.5 1.5 …
scale3d-150scale: 1.5 1.5 1.5

Inherits values from theme.scale in your config.

Scaling in one dimension:

<!-- scale on x-axis 50% -->
<div class="scale-x-50">

<!-- scale on y-axis 75% -->
<div class="scale-y-75">

<!-- scale on z-axis 110% -->
<div class="scale-z-110">

You can mix scaling values together as well:

<!-- scale on y-axis 75% and z-axis 95% -->
<div class="scale-y-75 scale-z-95"></div>

Arbitrary values:

<!-- scale on y-axis 30% -->
<div class="scale-y-[0.3]"></div>

Scale on both x and y-axis in proportion:

<!-- scale on x-axis and y-axis 50% -->
<div class="scale-50">

Scale all three dimensions in proportion:

<!-- scale on x-axis, y-axis and z-axis 150% -->
<div class="scale3d-150">

rotate

Tailwind Play rotate utility examples

ClassProperties
rotate-x-0transform: rotateX(0deg);
rotate-y-0transform: rotateY(0deg);
rotate-z-0rotate: 0deg;
rotate-0rotate: 0deg;
rotate-x-1transform: rotateX(1deg);
rotate-y-1transform: rotateY(1deg);
rotate-z-1rotate: 1deg;
rotate-1rotate: 1deg;
rotate-x-2transform: rotateX(2deg);
rotate-y-2transform: rotateY(2deg);
rotate-z-2rotate: 2deg;
rotate-2rotate: 2deg;
rotate-x-3transform: rotateX(3deg);
rotate-y-3transform: rotateY(3deg);
rotate-z-3rotate: 3deg;
rotate-3rotate: 3deg;
rotate-x-6transform: rotateX(6deg);
rotate-y-6transform: rotateY(6deg);
rotate-z-6rotate: 6deg;
rotate-6rotate: 6deg;
rotate-x-12transform: rotateX(12deg);
rotate-y-12transform: rotateY(12deg);
rotate-z-12rotate: 12deg;
rotate-12rotate: 12deg;
rotate-x-45transform: rotateX(45deg);
rotate-y-45transform: rotateY(45deg);
rotate-z-45rotate: 45deg;
rotate-45rotate: 45deg;
rotate-x-90transform: rotateX(90deg);
rotate-y-90transform: rotateY(90deg);
rotate-z-90rotate: 90deg;
rotate-90rotate: 90deg;
rotate-x-180transform: rotateX(180deg);
rotate-y-180transform: rotateY(180deg);
rotate-z-180rotate: 180deg;
rotate-180rotate: 180deg;

Inherits values from theme.rotate in your config.

Rotating around x-axis and y-axis:

<!-- rotate along x-axis 45 degrees -->
<div class="rotate-x-45"></div>

<!-- rotate along y-axis 12 degrees -->
<div class="rotate-y-12"></div>

Z-axis rotation is still the default, so the following are equivalent to each other:

<!-- rotate along z-axis 45 degrees -->
<div class="rotate-45"></div>

<!-- rotate along z-axis 45 degrees -->
<div class="rotate-z-45"></div>

Arbitrary values:

<!-- rotate along y-axis 30 degrees -->
<div class="rotate-y-[30deg]"></div>

You can mix rotation values together as well:

<!-- rotate along y-axis 12 degrees and z-axis 45 degrees -->
<div class="rotate-y-12 rotate-z-45"></div>

translate

Tailwind Play translate utility examples

ClassProperties
translate-x-0translate: 0px … …;
translate-y-0translate: … 0px …;
translate-z-0translate: … … 0px;
translate-x-pxtranslate: 1px … …;
translate-y-pxtranslate: … 1px …;
translate-z-pxtranslate: … … 1px;
translate-x-0.5translate: 0.125rem … …;
translate-y-0.5translate: … 0.125rem …;
translate-z-0.5translate: … … 0.125rem;
translate-x-1translate: 0.25rem … …;
translate-y-1translate: … 0.25rem …;
translate-z-1translate: … … 0.25rem;
translate-x-1.5translate: 0.375rem … …;
translate-y-1.5translate: … 0.375rem …;
translate-z-1.5translate: … … 0.375rem;
translate-x-2translate: 0.5rem … …;
translate-y-2translate: … 0.5rem …;
translate-z-2translate: … … 0.5rem;
translate-x-2.5translate: 0.625rem … …;
translate-y-2.5translate: … 0.625rem …;
translate-z-2.5translate: … … 0.625rem;
translate-x-3translate: 0.75rem … …;
translate-y-3translate: … 0.75rem …;
translate-z-3translate: … … 0.75rem;
translate-x-3.5translate: 0.875rem … …;
translate-y-3.5translate: … 0.875rem …;
translate-z-3.5translate: … … 0.875rem;
translate-x-4translate: 1rem … …;
translate-y-4translate: … 1rem …;
translate-z-4translate: … … 1rem;
translate-x-5translate: 1.25rem … …;
translate-y-5translate: … 1.25rem …;
translate-z-5translate: … … 1.25rem;
translate-x-6translate: 1.5rem … …;
translate-y-6translate: … 1.5rem …;
translate-z-6translate: … … 1.5rem;
translate-x-7translate: 1.75rem … …;
translate-y-7translate: … 1.75rem …;
translate-z-7translate: … … 1.75rem;
translate-x-8translate: 2rem … …;
translate-y-8translate: … 2rem …;
translate-z-8translate: … … 2rem;
translate-x-9translate: 2.25rem … …;
translate-y-9translate: … 2.25rem …;
translate-z-9translate: … … 2.25rem;
translate-x-10translate: 2.5rem … …;
translate-y-10translate: … 2.5rem …;
translate-z-10translate: … … 2.5rem;
translate-x-11translate: 2.75rem … …;
translate-y-11translate: … 2.75rem …;
translate-z-11translate: … … 2.75rem;
translate-x-12translate: 3rem … …;
translate-y-12translate: … 3rem …;
translate-z-12translate: … … 3rem;
translate-x-14translate: 3.5rem … …;
translate-y-14translate: … 3.5rem …;
translate-z-14translate: … … 3.5rem;
translate-x-16translate: 4rem … …;
translate-y-16translate: … 4rem …;
translate-z-16translate: … … 4rem;
translate-x-20translate: 5rem … …;
translate-y-20translate: … 5rem …;
translate-z-20translate: … … 5rem;
translate-x-24translate: 6rem … …;
translate-y-24translate: … 6rem …;
translate-z-24translate: … … 6rem;
translate-x-28translate: 7rem … …;
translate-y-28translate: … 7rem …;
translate-z-28translate: … … 7rem;
translate-x-32translate: 8rem … …;
translate-y-32translate: … 8rem …;
translate-z-32translate: … … 8rem;
translate-x-36translate: 9rem … …;
translate-y-36translate: … 9rem …;
translate-z-36translate: … … 9rem;
translate-x-40translate: 10rem … …;
translate-y-40translate: … 10rem …;
translate-z-40translate: … … 10rem;
translate-x-44translate: 11rem … …;
translate-y-44translate: … 11rem …;
translate-z-44translate: … … 11rem;
translate-x-48translate: 12rem … …;
translate-y-48translate: … 12rem …;
translate-z-48translate: … … 12rem;
translate-x-52translate: 13rem … …;
translate-y-52translate: … 13rem …;
translate-z-52translate: … … 13rem;
translate-x-56translate: 14rem … …;
translate-y-56translate: … 14rem …;
translate-z-56translate: … … 14rem;
translate-x-60translate: 15rem … …;
translate-y-60translate: … 15rem …;
translate-z-60translate: … … 15rem;
translate-x-64translate: 16rem … …;
translate-y-64translate: … 16rem …;
translate-z-64translate: … … 16rem;
translate-x-72translate: 18rem … …;
translate-y-72translate: … 18rem …;
translate-z-72translate: … … 18rem;
translate-x-80translate: 20rem … …;
translate-y-80translate: … 20rem …;
translate-z-80translate: … … 20rem;
translate-x-96translate: 24rem … …;
translate-y-96translate: … 24rem …;
translate-z-96translate: … … 24rem;
translate-x-1/2translate: 50% … …;
translate-y-1/2translate: … 50% …;
translate-x-1/3translate: 33.333333% … …;
translate-y-1/3translate: … 33.333333% …;
translate-x-2/3translate: 66.666667% … …;
translate-y-2/3translate: … 66.666667% …;
translate-x-1/4translate: 25% … …;
translate-y-1/4translate: … 25% …;
translate-x-2/4translate: 50% … …;
translate-y-2/4translate: … 50% …;
translate-x-3/4translate: 75% … …;
translate-y-3/4translate: … 75% …;
translate-x-fulltranslate: 100% … …;
translate-y-fulltranslate: … 100% …;

Inherits values from theme.translate in your config.

Note that translation on the z-axis can not use percentage values.

skew

ClassProperties
skew-x-0transform: skewX(0deg);
skew-y-0transform: skewY(0deg);
skew-x-1transform: skewX(1deg);
skew-y-1transform: skewY(1deg);
skew-x-2transform: skewX(2deg);
skew-y-2transform: skewY(2deg);
skew-x-3transform: skewX(3deg);
skew-y-3transform: skewY(3deg);
skew-x-6transform: skewX(6deg);
skew-y-6transform: skewY(6deg);
skew-x-12transform: skewX(12deg);
skew-y-12transform: skewY(12deg);

Inherits values from theme.skew in your config.

perspective

ClassProperties
perspective-noneperspective: none;
perspective-250perspective: 250px;
perspective-500perspective: 500px;
perspective-750perspective: 750px;
perspective-1000perspective: 1000px;

Inherits values from theme.perspective in your config.

perspective-origin

ClassProperties
perspective-origin-centerperspective-origin: center;
perspective-origin-topperspective-origin: top;
perspective-origin-top-rightperspective-origin: top right;
perspective-origin-rightperspective-origin: right;
perspective-origin-bottom-rightperspective-origin: bottom right;
perspective-origin-bottomperspective-origin: bottom;
perspective-origin-bottom-leftperspective-origin: bottom left;
perspective-origin-leftperspective-origin: left;
perspective-origin-top-leftperspective-origin: top left;

Inherits values from theme.perspectiveOrigin in your config.

backface

ClassProperties
backface-visiblebackface-visibility: visible;
backface-hiddenbackface-visibility: hidden;

Inherits values from theme.backface in your config.

transform-box

ClassProperties
transform-box-contenttransform-box: content-box
transform-box-bordertransform-box: border-box
transform-box-filltransform-box: fill-box
transform-box-stroketransform-box: stroke-box
transform-box-viewtransform-box: view-box

Inherits values from theme.transformBox in your config.

transform-style

ClassProperties
transform-style-flattransform-style: flat
transform-style-3dtransform-style: preserve-3d

Inherits values from theme.transformStyle in your config.

Animations

Tailwind Play animation examples

Available animations:

  • bounce (x, y, z)
  • spin (x, y, z)
  • bounce-and-spin (x, y, z)

An example:

<!-- spin around the y-axis at a medium rate -->
<div class="animate-spin-y-5"></div>

Note that you may see unexpected results on the y-axis as this plugin treats "up" as a negative value. You can use negative animations (prepending a - to the start of a class) to change the direction of travel:

<!-- bounce upwards at a medium rate and height on the y-axis -->
<div class="-animate-bounce-y-5"></div>

Configuration

Default modifiers and values for utilities and animations can be changed in your tailwind.config.js file by extending the relevant theme defaults.

Development

Requirements:

  • NodeJS 18+
  • PNPM 7+

Build the plugin

After cloning the repository, to compile a build:

pnpm build
pnpm build:types

This will create a compiled version of the plugin in the ./dist directory.

To have the build automatically compiled, run:

pnpm dev

This also compiles both code and types to ./dist.

Testing and linting

pnpm lint
pnpm test
1.0.6

1 month ago

1.0.5

3 months ago

1.0.4

5 months ago

1.0.3

6 months ago

1.0.2

7 months ago

1.0.1

8 months ago

1.0.0

9 months ago

0.3.0

9 months ago

0.2.6

10 months ago

0.3.1

9 months ago

0.2.5

10 months ago

0.2.4

11 months ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago