Skip to content

Feature/container max width #27

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

Closed
Closed
Changes from 1 commit
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
Next Next commit
add variants for container max-width queries
  • Loading branch information
bkdiehl committed May 14, 2024
commit 4f6d3a340aea66470e7d81afc85b7c278f340e5f
82 changes: 51 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
import plugin from 'tailwindcss/plugin'

type VariantSortProps = {
value: string;
modifier: string | null;
};

export = plugin(
function containerQueries({ matchUtilities, matchVariant, theme }) {
let values: Record<string, string> = theme('containers') ?? {}
const values: Record<string, string> = theme('containers') ?? {};

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

return parseFloat(value);
}

function sort(aVariant: VariantSortProps, zVariant: VariantSortProps) {
const a = parseFloat(aVariant.value);
const z = parseFloat(zVariant.value);

if (a === null || z === null) return 0;

return parseFloat(value)
// Sort values themselves regardless of unit
if (a - z !== 0) return a - z;

const aLabel = aVariant.modifier ?? '';
const zLabel = zVariant.modifier ?? '';

// Explicitly move empty labels to the end
if (aLabel === '' && zLabel !== '') {
return 1;
} else if (aLabel !== '' && zLabel === '') {
return -1;
}

// Sort labels alphabetically in the English locale
// We are intentionally overriding the locale because we do not want the sort to
// be affected by the machine's locale (be it a developer or CI environment)
return aLabel.localeCompare(zLabel, 'en', { numeric: true });
}

matchUtilities(
Expand All @@ -17,7 +47,7 @@ export = plugin(
return {
'container-type': value,
'container-name': modifier,
}
};
},
},
{
Expand All @@ -27,43 +57,33 @@ export = plugin(
},
modifiers: 'any',
}
)
);

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

return parsed !== null ? `@container ${modifier ?? ''} (min-width: ${value})` : []
return parsed !== null ? `@container ${modifier ?? ''} (min-width: ${value})` : [];
},
{
values,
sort(aVariant, zVariant) {
let a = parseFloat(aVariant.value)
let z = parseFloat(zVariant.value)

if (a === null || z === null) return 0

// Sort values themselves regardless of unit
if (a - z !== 0) return a - z

let aLabel = aVariant.modifier ?? ''
let zLabel = zVariant.modifier ?? ''
sort,
}
);

// Explicitly move empty labels to the end
if (aLabel === '' && zLabel !== '') {
return 1
} else if (aLabel !== '' && zLabel === '') {
return -1
}
matchVariant(
'@max',
(value = '', { modifier }) => {
const parsed = parseValue(value);

// Sort labels alphabetically in the English locale
// We are intentionally overriding the locale because we do not want the sort to
// be affected by the machine's locale (be it a developer or CI environment)
return aLabel.localeCompare(zLabel, 'en', { numeric: true })
},
return parsed !== null ? `@container ${modifier ?? ''} (width < ${value})` : [];
},
{
values,
sort,
}
)
);
},
{
theme: {
Expand Down