Skip to content

Commit 1c54dc3

Browse files
committed
chore: Make all global selectors apply uniformly
1 parent a168ef5 commit 1c54dc3

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/shared/declaration/__tests__/selector.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('Selector', () => {
2020
test('creates selector for context', () => {
2121
expect(selector.for({ global: ['.theme'], local: ['.context'] })).toEqual('.theme .context');
2222
expect(selector.for({ global: [':root'], local: ['.context'] })).toEqual('.context');
23+
expect(selector.for({ global: ['html', '.theme'], local: ['.context'] })).toEqual('.theme .context');
2324
});
2425

2526
test('creates selector for context within mode', () => {
@@ -34,4 +35,10 @@ describe('Selector', () => {
3435
const selector = new Selector((sel) => `${sel}:not(.theme)`);
3536
expect(selector.for({ global: [':root', '.mode'], local: ['.context'] })).toEqual('.mode .context:not(.theme)');
3637
});
37-
});
38+
39+
test('applies global selectors alone when no local selectors', () => {
40+
expect(selector.for({ global: [':root'] })).toEqual(':root');
41+
expect(selector.for({ global: ['body'] })).toEqual('body');
42+
expect(selector.for({ global: ['html'] })).toEqual('html');
43+
});
44+
});

src/shared/declaration/selector.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33
import type { SelectorCustomizer } from './interfaces';
4+
import { isGlobalSelector } from '../styles/selector';
45

56
interface SelectorParams {
67
global: string[];
@@ -15,13 +16,13 @@ export class Selector {
1516
}
1617

1718
for({ global, local }: SelectorParams): string {
18-
if (global.length === 1 && !local?.length && global[0] === ':root') {
19-
// :root is only applied alone
20-
return this.customizer(':root');
19+
if (global.length === 1 && !local?.length && isGlobalSelector(global[0])) {
20+
// Global selectors (:root, body, html) are only applied alone
21+
return this.customizer(global[0]);
2122
}
22-
const globalWithoutRoot = global.filter((f) => f !== ':root');
23+
const customGlobal = global.filter((f) => !isGlobalSelector(f));
2324

24-
let selector = this.toSelector(globalWithoutRoot);
25+
let selector = this.toSelector(customGlobal);
2526
if (local?.length) {
2627
selector += ` ${this.toSelector(local)}`;
2728
}

0 commit comments

Comments
 (0)