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

Consider all prefixes as valid Tailwind prefixes #50

Merged
merged 5 commits into from
Oct 21, 2021
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
10 changes: 5 additions & 5 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ version-resolver:
autolabeler:
- label: 'feature'
branch:
- '/feature/i'
- '/\bfeature\b/i'
title:
- '/feature/i'
- '/\bfeature\b/i'
- label: 'bugfix'
branch:
- '/fix/i'
- '/\b(bugfix|fix)\b/i'
title:
- '/fix/i'
- '/\b(bugfix|fix)\b/i'
- label: 'other'
branch:
- '/^other/i'
- '/^other\b/i'
- label: 'breaking'
branch:
- '/^breaking-/i'
2 changes: 0 additions & 2 deletions src/config-utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { getLruCache } from './lru-cache'
import { Config } from './types'
import { createClassUtils } from './class-utils'
import { createPrefixUtils } from './prefix-utils'

export type ConfigUtils = ReturnType<typeof createConfigUtils>

export function createConfigUtils(config: Config) {
return {
cache: getLruCache<string>(config.cacheSize),
...createPrefixUtils(config),
...createClassUtils(config),
}
}
57 changes: 0 additions & 57 deletions src/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,66 +51,9 @@ export function getDefaultConfig() {
] as const
const getAlign = () => ['start', 'end', 'center', 'between', 'around', 'evenly'] as const
const getZeroAndEmpty = () => ['', '0'] as const
const getPseudoVariants = () =>
[
// Positional
'first',
'last',
'only',
'odd',
'even',
'first-of-type',
'last-of-type',
'only-of-type',

// State
'visited',
'target',

// Forms
'default',
'checked',
'indeterminate',
'placeholder-shown',
'autofill',
'required',
'valid',
'invalid',
'in-range',
'out-of-range',
'read-only',

// Content
'empty',

// Interactive
'focus-within',
'hover',
'focus',
'focus-visible',
'active',
'disabled',
] as const

return {
cacheSize: 500,
prefixes: [
...getSizesSimple(),
'dark',
'motion-safe',
'motion-reduce',
'before',
'after',
'first-letter',
'first-line',
'selection',
'marker',
...getPseudoVariants(),
{
group: getPseudoVariants(),
peer: getPseudoVariants(),
},
],
classGroups: {
// Layout
/**
Expand Down
16 changes: 6 additions & 10 deletions src/merge-classlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ const PREFIX_SEPARATOR_REGEX = /:(?![^[]*\])/
const PREFIX_SEPARATOR = ':'

export function mergeClassList(classList: string, configUtils: ConfigUtils) {
const { isPrefixValid, getClassGroupId, comparePrefixes, getConflictingClassGroupIds } =
configUtils
const { getClassGroupId, getConflictingClassGroupIds } = configUtils

/**
* Set of classGroupIds in following format:
* `{importantModifier}{variantPrefixes}{classGroupId}`
* @example ':standaloneClasses.1'
* @example 'hover:focus:dynamicClasses.bg.2'
* @example '!md:dynamicClasses.bg.0'
* @example 'float'
* @example 'hover:focus:bg-color'
* @example '!md:pr'
*/
const classGroupsInConflict = new Set<string>()

Expand All @@ -34,8 +33,7 @@ export function mergeClassList(classList: string, configUtils: ConfigUtils) {
? classNameWithImportantModifier.substring(1)
: classNameWithImportantModifier

const arePrefixesValid = prefixes.every(isPrefixValid)
const classGroupId = arePrefixesValid ? getClassGroupId(className) : undefined
const classGroupId = getClassGroupId(className)

if (!classGroupId) {
return {
Expand All @@ -45,9 +43,7 @@ export function mergeClassList(classList: string, configUtils: ConfigUtils) {
}

const variantPrefix =
prefixes.length === 0
? ''
: prefixes.sort(comparePrefixes).concat('').join(PREFIX_SEPARATOR)
prefixes.length === 0 ? '' : prefixes.sort().concat('').join(PREFIX_SEPARATOR)

const fullPrefix = hasImportantModifier
? IMPORTANT_MODIFIER + variantPrefix
Expand Down
34 changes: 0 additions & 34 deletions src/prefix-utils.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
export interface Config {
/**
* Prefixes which can be prepended to Tailwind CSS classes
* @example ['hover', 'focus']
*/
prefixes: readonly Prefix[]
/**
* Integer indicating size of LRU cache used for memoizing results.
* - Cache might be up to twice as big as `cacheSize`
Expand All @@ -30,7 +25,6 @@ export interface Config {
conflictingClassGroups: Record<ClassGroupId, readonly ClassGroupId[]>
}

export type Prefix = string | Record<string, readonly Prefix[]>
export type ClassGroup = readonly ClassDefinition[]
type ClassDefinition = string | ClassValidator | ClassObject
export type ClassValidator = (classPart: string) => boolean
Expand Down
7 changes: 2 additions & 5 deletions tests/create-tailwind-merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { createTailwindMerge } from '../src'
test('createTailwindMerge works with single config function', () => {
const tailwindMerge = createTailwindMerge(() => ({
cacheSize: 20,
prefixes: ['my-prefix'],
classGroups: {
fooKey: [{ fooKey: ['bar', 'baz'] }],
fooKey2: [{ fooKey: ['qux', 'quux'] }, 'other-2'],
Expand All @@ -18,7 +17,7 @@ test('createTailwindMerge works with single config function', () => {
expect(tailwindMerge('')).toBe('')
expect(tailwindMerge('my-prefix:fooKey-bar my-prefix:fooKey-baz')).toBe('my-prefix:fooKey-baz')
expect(tailwindMerge('other-prefix:fooKey-bar other-prefix:fooKey-baz')).toBe(
'other-prefix:fooKey-bar other-prefix:fooKey-baz'
'other-prefix:fooKey-baz'
)
expect(tailwindMerge('group fooKey-bar')).toBe('fooKey-bar')
expect(tailwindMerge('fooKey-bar group')).toBe('group')
Expand All @@ -40,7 +39,6 @@ test('createTailwindMerge works with multiple config functions', () => {
const tailwindMerge = createTailwindMerge(
() => ({
cacheSize: 20,
prefixes: ['my-prefix'],
classGroups: {
fooKey: [{ fooKey: ['bar', 'baz'] }],
fooKey2: [{ fooKey: ['qux', 'quux'] }, 'other-2'],
Expand All @@ -53,7 +51,6 @@ test('createTailwindMerge works with multiple config functions', () => {
}),
(config) => ({
...config,
prefixes: [...config.prefixes, 'second'],
classGroups: {
...config.classGroups,
helloFromSecondConfig: ['hello-there'],
Expand All @@ -68,7 +65,7 @@ test('createTailwindMerge works with multiple config functions', () => {
expect(tailwindMerge('')).toBe('')
expect(tailwindMerge('my-prefix:fooKey-bar my-prefix:fooKey-baz')).toBe('my-prefix:fooKey-baz')
expect(tailwindMerge('other-prefix:fooKey-bar other-prefix:fooKey-baz')).toBe(
'other-prefix:fooKey-bar other-prefix:fooKey-baz'
'other-prefix:fooKey-baz'
)
expect(tailwindMerge('group fooKey-bar')).toBe('fooKey-bar')
expect(tailwindMerge('fooKey-bar group')).toBe('group')
Expand Down
12 changes: 6 additions & 6 deletions tests/extend-tailwind-merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { extendTailwindMerge } from '../src'
test('extendTailwindMerge works corectly with single config', () => {
const tailwindMerge = extendTailwindMerge({
cacheSize: 20,
prefixes: ['my-prefix'],
classGroups: {
fooKey: [{ fooKey: ['bar', 'baz'] }],
fooKey2: [{ fooKey: ['qux', 'quux'] }, 'other-2'],
Expand All @@ -18,7 +17,7 @@ test('extendTailwindMerge works corectly with single config', () => {
expect(tailwindMerge('')).toBe('')
expect(tailwindMerge('my-prefix:fooKey-bar my-prefix:fooKey-baz')).toBe('my-prefix:fooKey-baz')
expect(tailwindMerge('other-prefix:fooKey-bar other-prefix:fooKey-baz')).toBe(
'other-prefix:fooKey-bar other-prefix:fooKey-baz'
'other-prefix:fooKey-baz'
)
expect(tailwindMerge('group fooKey-bar')).toBe('fooKey-bar')
expect(tailwindMerge('fooKey-bar group')).toBe('group')
Expand All @@ -45,15 +44,16 @@ test('extendTailwindMerge works corectly with multiple configs', () => {
},
(config) => ({
...config,
prefixes: [...config.prefixes, 'my-prefix'],
classGroups: {
...config.classGroups,
secondConfigKey: ['hi-there', 'hello'],
},
})
)

expect(tailwindMerge('')).toBe('')
expect(tailwindMerge('my-prefix:fooKey-bar my-prefix:fooKey-baz')).toBe('my-prefix:fooKey-baz')
expect(tailwindMerge('other-prefix:fooKey-bar other-prefix:fooKey-baz')).toBe(
'other-prefix:fooKey-bar other-prefix:fooKey-baz'
)
expect(tailwindMerge('other-prefix:hi-there other-prefix:hello')).toBe('other-prefix:hello')
expect(tailwindMerge('group fooKey-bar')).toBe('fooKey-bar')
expect(tailwindMerge('fooKey-bar group')).toBe('group')
expect(tailwindMerge('group other-2')).toBe('group other-2')
Expand Down
3 changes: 0 additions & 3 deletions tests/merge-configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ test('mergeConfigs has correct behavior', () => {
mergeConfigs(
{
cacheSize: 50,
prefixes: ['my-prefix'],
classGroups: {
fooKey: [{ fooKey: ['one', 'two'] }],
bla: [{ bli: ['blub', 'blublub'] }],
},
conflictingClassGroups: {},
},
{
prefixes: ['my-prefix-2'],
classGroups: {
fooKey: [{ fooKey: ['bar', 'baz'] }],
fooKey2: [{ fooKey: ['qux', 'quux'] }],
Expand All @@ -27,7 +25,6 @@ test('mergeConfigs has correct behavior', () => {
)
).toEqual({
cacheSize: 50,
prefixes: ['my-prefix', 'my-prefix-2'],
classGroups: {
fooKey: [{ fooKey: ['one', 'two'] }, { fooKey: ['bar', 'baz'] }],
bla: [{ bli: ['blub', 'blublub'] }],
Expand Down
1 change: 0 additions & 1 deletion tests/public-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ test('mergeConfigs has correct inputs and outputs', () => {
mergeConfigs(
{
cacheSize: 50,
prefixes: ['my-prefix'],
classGroups: {
fooKey: [{ fooKey: ['one', 'two'] }],
bla: [{ bli: ['blub', 'blublub'] }],
Expand Down