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(preset-mini): customizing dark mode selector #1250

Merged
merged 7 commits into from Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 13 additions & 1 deletion packages/preset-mini/src/index.ts
Expand Up @@ -11,11 +11,23 @@ export { parseColor } from './utils'

export type { ThemeAnimation, Theme }

type DarkModeSelectors = ['class', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a simple object is enough, the array feels redundant to me (plus they are not always class anyway)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as I thought, not sure why tailwindicss needs to set a class

/**
* @default '.light'
*/
light?: string

/**
* @default '.dark'
*/
dark?: string
}]

export interface PresetMiniOptions extends PresetOptions {
/**
* @default 'class'
*/
dark?: 'class' | 'media'
dark?: 'class' | 'media' | DarkModeSelectors
/**
* @default false
*/
Expand Down
7 changes: 4 additions & 3 deletions packages/preset-mini/src/variants/dark.ts
Expand Up @@ -3,10 +3,11 @@ import type { PresetMiniOptions } from '..'
import { variantMatcher, variantParentMatcher } from '../utils'

export const variantColorsMediaOrClass = (options: PresetMiniOptions = {}): Variant[] => {
if (options?.dark === 'class') {
if (options?.dark === 'class' || options?.dark?.[0] === 'class') {
const { dark = '.dark', light = '.light' } = typeof options.dark === 'string' ? {} : options.dark[1]
return [
variantMatcher('dark', input => ({ prefix: `.dark $$ ${input.prefix}` })),
variantMatcher('light', input => ({ prefix: `.light $$ ${input.prefix}` })),
variantMatcher('dark', input => ({ prefix: `${dark} $$ ${input.prefix}` })),
variantMatcher('light', input => ({ prefix: `${light} $$ ${input.prefix}` })),
]
}

Expand Down
10 changes: 10 additions & 0 deletions test/__snapshots__/preset-mini.test.ts.snap
Expand Up @@ -7,6 +7,16 @@ exports[`preset-mini > custom var prefix 1`] = `
.scale-100{--hi-scale-x:1;--hi-scale-y:1;transform:translateX(var(--hi-translate-x)) translateY(var(--hi-translate-y)) translateZ(var(--hi-translate-z)) rotate(var(--hi-rotate)) rotateX(var(--hi-rotate-x)) rotateY(var(--hi-rotate-y)) rotateZ(var(--hi-rotate-z)) skewX(var(--hi-skew-x)) skewY(var(--hi-skew-y)) scaleX(var(--hi-scale-x)) scaleY(var(--hi-scale-y)) scaleZ(var(--hi-scale-z));}"
`;

exports[`preset-mini > dark customizing selector 1`] = `
"/* layer: default */
[data-mode=\\"dark\\"] .dark\\\\:bg-white{--un-bg-opacity:1;background-color:rgba(255,255,255,var(--un-bg-opacity));}
[data-mode=\\"light\\"] .light\\\\:bg-black{--un-bg-opacity:1;background-color:rgba(0,0,0,var(--un-bg-opacity));}
[data-mode=\\"dark\\"] .dark\\\\:hover\\\\:rounded:hover{border-radius:0.25rem;}
[data-mode=\\"dark\\"] .dark\\\\:text-lg{font-size:1.125rem;line-height:1.75rem;}
[data-mode=\\"light\\"] .light\\\\:text-sm{font-size:0.875rem;line-height:1.25rem;}
[data-mode=\\"light\\"] .light\\\\:disabled\\\\:w-full:disabled{width:100%;}"
`;

exports[`preset-mini > nested theme colors 1`] = `
"/* layer: default */
.bg-a-b-c{--un-bg-opacity:1;background-color:rgba(81,69,67,var(--un-bg-opacity));}
Expand Down
26 changes: 26 additions & 0 deletions test/preset-mini.test.ts
Expand Up @@ -27,6 +27,32 @@ const uno = createGenerator({
})

describe('preset-mini', () => {
test('dark customizing selector', async () => {
const uno = createGenerator({
presets: [
presetMini({
dark: ['class', {
dark: '[data-mode="dark"]',
light: '[data-mode="light"]',
}],
}),
],
})

const { css } = await uno.generate([
'dark:bg-white',
'dark:text-lg',
'dark:hover:rounded',
'light:bg-black',
'light:text-sm',
'light:disabled:w-full',
].join(' '), {
preflights: false,
})

expect(css).toMatchSnapshot()
})

test('targets', async () => {
const code = presetMiniTargets.join(' ')
const { css } = await uno.generate(code)
Expand Down