Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add arbitrary value support #13

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ There are no any `container-name` utilities by default. You should define your o
}
```

#### You can use arbitrary variants too. (You should be using v3.1.0 or higher!)

```html
<div class="cq-w-[238px]:bg-yellow-400"></div>
```

`.cq-w-6:bg-yellow-400` output:

```css
@container (min-width: 238px) {
.cq-w-\[238px\]\:bg-yellow-400 {
--tw-bg-opacity: 1;
background-color: rgb(96 165 250 / var(--tw-bg-opacity));
}
}
```

## Using Container Names

```html
Expand Down
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</head>
<body>
<div class="container-type-size w-80 h-80 overflow-auto resize">
<div class="bg-yellow-100 h-full w-full border-4 border-gray-100 cq-w-22:bg-blue-200 cq-h-22:border-red-400 flex justify-end items-end">
<div class="bg-yellow-100 h-full w-full border-4 border-gray-100 cq-w-22:bg-blue-200 cq-h-[111px]:border-red-400 flex justify-end items-end">
<div class="inline-flex items-end">
<div class="pb-12">Resize the container</div>
<svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="48" height="48" fill="white" fill-opacity="0.01"/><path d="M37 19V37H19" stroke="#333" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M11.5441 11.5442L36.9999 37" stroke="#333" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
Expand Down
179 changes: 151 additions & 28 deletions demo/output.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
! tailwindcss v3.0.0 | MIT License | https://tailwindcss.com
! tailwindcss v3.1.4 | MIT License | https://tailwindcss.com
*/

/*
Expand All @@ -16,7 +16,7 @@
/* 2 */
border-style: solid;
/* 2 */
border-color: currentColor;
border-color: #e5e7eb;
/* 2 */
}

Expand Down Expand Up @@ -77,7 +77,7 @@ hr {
Add the correct text decoration in Chrome, Edge, and Safari.
*/

abbr[title] {
abbr:where([title]) {
-webkit-text-decoration: underline dotted;
text-decoration: underline dotted;
}
Expand Down Expand Up @@ -187,6 +187,8 @@ textarea {
/* 1 */
font-size: 100%;
/* 1 */
font-weight: inherit;
/* 1 */
line-height: inherit;
/* 1 */
color: inherit;
Expand Down Expand Up @@ -353,13 +355,6 @@ input::-moz-placeholder, textarea::-moz-placeholder {
/* 2 */
}

input:-ms-input-placeholder, textarea:-ms-input-placeholder {
opacity: 1;
/* 1 */
color: #9ca3af;
/* 2 */
}

input::placeholder,
textarea::placeholder {
opacity: 1;
Expand Down Expand Up @@ -415,17 +410,145 @@ video {
height: auto;
}

/*
Ensure the default browser behavior of the `hidden` attribute.
*/

[hidden] {
display: none;
}

.border-4 {
--tw-border-opacity: 1;
border-color: rgb(229 231 235 / var(--tw-border-opacity));
*, ::before, ::after {
--tw-border-spacing-x: 0;
--tw-border-spacing-y: 0;
--tw-translate-x: 0;
--tw-translate-y: 0;
--tw-rotate: 0;
--tw-skew-x: 0;
--tw-skew-y: 0;
--tw-scale-x: 1;
--tw-scale-y: 1;
--tw-pan-x: ;
--tw-pan-y: ;
--tw-pinch-zoom: ;
--tw-scroll-snap-strictness: proximity;
--tw-ordinal: ;
--tw-slashed-zero: ;
--tw-numeric-figure: ;
--tw-numeric-spacing: ;
--tw-numeric-fraction: ;
--tw-ring-inset: ;
--tw-ring-offset-width: 0px;
--tw-ring-offset-color: #fff;
--tw-ring-color: rgb(59 130 246 / 0.5);
--tw-ring-offset-shadow: 0 0 #0000;
--tw-ring-shadow: 0 0 #0000;
--tw-shadow: 0 0 #0000;
--tw-shadow-colored: 0 0 #0000;
--tw-blur: ;
--tw-brightness: ;
--tw-contrast: ;
--tw-grayscale: ;
--tw-hue-rotate: ;
--tw-invert: ;
--tw-saturate: ;
--tw-sepia: ;
--tw-drop-shadow: ;
--tw-backdrop-blur: ;
--tw-backdrop-brightness: ;
--tw-backdrop-contrast: ;
--tw-backdrop-grayscale: ;
--tw-backdrop-hue-rotate: ;
--tw-backdrop-invert: ;
--tw-backdrop-opacity: ;
--tw-backdrop-saturate: ;
--tw-backdrop-sepia: ;
}

::-webkit-backdrop {
--tw-border-spacing-x: 0;
--tw-border-spacing-y: 0;
--tw-translate-x: 0;
--tw-translate-y: 0;
--tw-rotate: 0;
--tw-skew-x: 0;
--tw-skew-y: 0;
--tw-scale-x: 1;
--tw-scale-y: 1;
--tw-pan-x: ;
--tw-pan-y: ;
--tw-pinch-zoom: ;
--tw-scroll-snap-strictness: proximity;
--tw-ordinal: ;
--tw-slashed-zero: ;
--tw-numeric-figure: ;
--tw-numeric-spacing: ;
--tw-numeric-fraction: ;
--tw-ring-inset: ;
--tw-ring-offset-width: 0px;
--tw-ring-offset-color: #fff;
--tw-ring-color: rgb(59 130 246 / 0.5);
--tw-ring-offset-shadow: 0 0 #0000;
--tw-ring-shadow: 0 0 #0000;
--tw-shadow: 0 0 #0000;
--tw-shadow-colored: 0 0 #0000;
--tw-blur: ;
--tw-brightness: ;
--tw-contrast: ;
--tw-grayscale: ;
--tw-hue-rotate: ;
--tw-invert: ;
--tw-saturate: ;
--tw-sepia: ;
--tw-drop-shadow: ;
--tw-backdrop-blur: ;
--tw-backdrop-brightness: ;
--tw-backdrop-contrast: ;
--tw-backdrop-grayscale: ;
--tw-backdrop-hue-rotate: ;
--tw-backdrop-invert: ;
--tw-backdrop-opacity: ;
--tw-backdrop-saturate: ;
--tw-backdrop-sepia: ;
}

::backdrop {
--tw-border-spacing-x: 0;
--tw-border-spacing-y: 0;
--tw-translate-x: 0;
--tw-translate-y: 0;
--tw-rotate: 0;
--tw-skew-x: 0;
--tw-skew-y: 0;
--tw-scale-x: 1;
--tw-scale-y: 1;
--tw-pan-x: ;
--tw-pan-y: ;
--tw-pinch-zoom: ;
--tw-scroll-snap-strictness: proximity;
--tw-ordinal: ;
--tw-slashed-zero: ;
--tw-numeric-figure: ;
--tw-numeric-spacing: ;
--tw-numeric-fraction: ;
--tw-ring-inset: ;
--tw-ring-offset-width: 0px;
--tw-ring-offset-color: #fff;
--tw-ring-color: rgb(59 130 246 / 0.5);
--tw-ring-offset-shadow: 0 0 #0000;
--tw-ring-shadow: 0 0 #0000;
--tw-shadow: 0 0 #0000;
--tw-shadow-colored: 0 0 #0000;
--tw-blur: ;
--tw-brightness: ;
--tw-contrast: ;
--tw-grayscale: ;
--tw-hue-rotate: ;
--tw-invert: ;
--tw-saturate: ;
--tw-sepia: ;
--tw-drop-shadow: ;
--tw-backdrop-blur: ;
--tw-backdrop-brightness: ;
--tw-backdrop-contrast: ;
--tw-backdrop-grayscale: ;
--tw-backdrop-hue-rotate: ;
--tw-backdrop-invert: ;
--tw-backdrop-opacity: ;
--tw-backdrop-saturate: ;
--tw-backdrop-sepia: ;
}

.container {
Expand Down Expand Up @@ -524,16 +647,16 @@ Ensure the default browser behavior of the `hidden` attribute.
container-type: size;
}

@container (min-height: 111px) {
.cq-h-\[111px\]\:border-red-400 {
--tw-border-opacity: 1;
border-color: rgb(248 113 113 / var(--tw-border-opacity));
}
}

@container (min-width: 352px) {
.cq-w-22\:bg-blue-200 {
--tw-bg-opacity: 1;
background-color: rgb(191 219 254 / var(--tw-bg-opacity));
}
}

@container (min-height: 352px) {
.cq-h-22\:border-red-400 {
--tw-border-opacity: 1;
border-color: rgb(248 113 113 / var(--tw-border-opacity));
}
}
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tailwindcss-container-query",
"version": "1.0.1",
"version": "1.1.0",
"description": "A plugin that provides CSS Container Queries.",
"main": "src/index.js",
"author": "Doğukan Çavuş <[email protected]>",
Expand All @@ -22,15 +22,15 @@
"prepublishOnly": "node scripts/build.js"
},
"peerDependencies": {
"tailwindcss": ">=2.0.0 || >=3.0.0 || >=3.0.0-alpha.1"
"tailwindcss": ">=3.1.0"
},
"devDependencies": {
"autoprefixer": "^10.4.2",
"clean-css": "^5.2.4",
"autoprefixer": "^10.4.7",
"clean-css": "^5.3.0",
"gh-pages": "^3.2.3",
"jest": "^27.5.1",
"jest": "^28.1.1",
"jest-matcher-css": "^1.1.0",
"postcss": "^8.4.7",
"tailwindcss": "^3.0.23"
"postcss": "^8.4.14",
"tailwindcss": "^3.1.4"
}
}
14 changes: 9 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const plugin = require('tailwindcss/plugin')

const containerQuery = plugin(
function ({ addUtilities, addVariant, theme, variants, e, postcss }) {
function ({ addUtilities, addVariant, matchVariant, theme, variants, e, postcss }) {
const containerType = theme('containerType')
const containerQuery = theme('containerQuery')
const containerName = theme('containerName')
Expand Down Expand Up @@ -32,6 +32,13 @@ const containerQuery = plugin(
variants('containerName')
)

matchVariant({
'cq-h': (queryValue) => `@container (min-height: ${queryValue})`,
})
matchVariant({
'cq-w': (queryValue) => `@container (min-width: ${queryValue})`,
})

Object.entries(containerQuery).map(([key, value]) => {
return addVariant(`cq-w-${key}`, ({ container, separator }) => {
const cqRule = postcss.atRule({ name: 'container', params: `(min-width: ${value})` })
Expand Down Expand Up @@ -81,6 +88,7 @@ const containerQuery = plugin(
})
},
{
experimental: { matchVariant: true },
theme: {
containerQuery: {
4: '64px',
Expand All @@ -98,10 +106,6 @@ const containerQuery = plugin(
},
containerName: {}
},
variants: {
containerType: ['responsive'],
containerName: [],
},
}
)

Expand Down
24 changes: 24 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ it('should add the container query at-rule for `.cq-h-22` class and its contents
})
})

it('should match the arbitrary values', () => {
const config = {
...defaultOptions,
content: [{ raw: String.raw`<div class="cq-h-[450px]:bg-yellow-200 cq-w-[238px]:bg-yellow-200"></div>` }],
}

return run('@tailwind utilities;', config).then((result) => {
expect(result.css).toMatchCss(String.raw`
@container (min-height: 450px) {
.cq-h-\[450px\]\:bg-yellow-200 {
--tw-bg-opacity: 1;
background-color: rgb(254 240 138 / var(--tw-bg-opacity));
}
}
@container (min-width: 238px) {
.cq-w-\[238px\]\:bg-yellow-200 {
--tw-bg-opacity: 1;
background-color: rgb(254 240 138 / var(--tw-bg-opacity));
}
}
`)
})
})

it('should add the `container-type-size` class', () => {
const config = {
...defaultOptions,
Expand Down
Loading