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

Added support for raw queries and container-type: size #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -2,3 +2,5 @@ node_modules
dist
yarn.lock
package-lock.json

.idea
20 changes: 16 additions & 4 deletions src/index.ts
Expand Up @@ -4,7 +4,7 @@ export = plugin(
function containerQueries({ matchUtilities, matchVariant, theme }) {
let values: Record<string, string> = theme('containers') ?? {}

function parseValue(value: string) {
function parseNumberValue(value: string) {
let numericValue = value.match(/^(\d+\.\d+|\d+|\.\d+)\D+/)?.[1] ?? null
if (numericValue === null) return null

Expand All @@ -24,17 +24,29 @@ export = plugin(
values: {
DEFAULT: 'inline-size',
normal: 'normal',
size: 'size'
},
modifiers: 'any',
}
)

matchVariant(
'@',
(value = '', { modifier }) => {
let parsed = parseValue(value)
let parsedNumber = null;
let parsedObject = null;

if(typeof value === 'object') {
parsedObject = JSON.parse(JSON.stringify(value)) || null;
}else{
parsedNumber = parseNumberValue(value);
}


if(parsedObject !== null) {
return `@container ${modifier ?? ''} ${parsedObject.raw}`
}

return parsed !== null ? `@container ${modifier ?? ''} (min-width: ${value})` : []
return parsedNumber !== null ? `@container ${modifier ?? ''} (min-width: ${value})` : []
},
{
values,
Expand Down
7 changes: 6 additions & 1 deletion tests/index.test.ts
Expand Up @@ -7,7 +7,7 @@ it('container queries', () => {
{
raw: html`
<div
class="@container @container-normal @container/sidebar @container-normal/sidebar @container-[size]/sidebar"
class="@container @container-normal @container-size @container/sidebar @container-normal/sidebar @container-[size]/sidebar"
>
<div class="@md:underline"></div>
<div class="@md/container1:underline"></div>
Expand Down Expand Up @@ -39,6 +39,7 @@ it('container queries', () => {
sm: '320px',
md: '768px',
lg: '1024px',
"xs-h": { raw: "(max-height: 600px)" },
},
},
corePlugins: { preflight: false },
Expand All @@ -58,6 +59,10 @@ it('container queries', () => {
container-type: normal;
}

.\@container-size {
container-type: size;
}

.\@container\/sidebar {
container-type: inline-size;
container-name: sidebar;
Expand Down