Skip to content

Commit 1710539

Browse files
committed
feat(tasty): selector affix for sub-elements * 2
1 parent 1177c1d commit 1710539

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/tasty/utils/renderStyles.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,27 @@ export function isSelector(key: string): boolean {
7272
);
7373
}
7474

75+
/**
76+
* Transform selector affix by converting capitalized words to sub-element selectors.
77+
* Returns a selector prefix with leading and trailing spaces.
78+
*
79+
* Examples:
80+
* '> Body > Row' -> ' > [data-element="Body"] > [data-element="Row"] '
81+
* '> Body > Row >' -> ' > [data-element="Body"] > [data-element="Row"] > '
82+
* '>' -> ' > '
83+
*/
84+
function transformSelectorAffix(affix: string): string {
85+
const trimmed = affix.trim();
86+
if (!trimmed) return ' ';
87+
88+
const tokens = trimmed.split(/\s+/);
89+
const transformed = tokens.map((token) =>
90+
/^[A-Z]/.test(token) ? `[data-element="${token}"]` : token,
91+
);
92+
93+
return ` ${transformed.join(' ')} `;
94+
}
95+
7596
/**
7697
* Get the selector suffix for a key
7798
*/
@@ -85,9 +106,12 @@ function getSelector(key: string, styles?: Styles): string | null {
85106
}
86107

87108
if (key.match(/^[A-Z]/)) {
88-
// Check if styles object has $: '>' for direct child selector
89-
const combinator = styles && (styles as any).$ === '>' ? ' > ' : ' ';
90-
return `${combinator}[data-element="${key}"]`;
109+
const affix = styles?.$;
110+
if (affix !== undefined) {
111+
const prefix = transformSelectorAffix(String(affix));
112+
return `${prefix}[data-element="${key}"]`;
113+
}
114+
return ` [data-element="${key}"]`;
91115
}
92116

93117
return null;

0 commit comments

Comments
 (0)