Skip to content

Commit 9c66fa5

Browse files
committed
feat(tasty): add createGlobalStyle method
1 parent 52161f9 commit 9c66fa5

File tree

5 files changed

+688
-2
lines changed

5 files changed

+688
-2
lines changed

src/components/GlobalStyles.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { createGlobalStyle } from 'styled-components';
2-
1+
import { createGlobalStyle } from '../tasty';
32
import { TOKENS } from '../tokens';
43

54
interface GlobalStylesProps {

src/tasty/injector/index.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { StyleResult } from '../utils/renderStyles';
66
import {
77
cleanup,
88
configure,
9+
createGlobalStyle,
910
createInjector,
1011
destroy,
1112
getCssText,
@@ -500,4 +501,31 @@ describe('Global Style Injector API', () => {
500501
expect(shadow2Css).not.toContain('color: red');
501502
});
502503
});
504+
505+
describe('createGlobalStyle', () => {
506+
test('exports createGlobalStyle function from global injector', () => {
507+
expect(typeof createGlobalStyle).toBe('function');
508+
509+
// Create a global style component
510+
const GlobalStyle = createGlobalStyle<{ color: string }>`
511+
.test-global {
512+
color: ${(props) => props.color};
513+
}
514+
`;
515+
516+
// Check that it returns a React component type
517+
expect(typeof GlobalStyle).toBe('function');
518+
expect(GlobalStyle.prototype).toBeDefined();
519+
520+
// Test that the interpolation would work as expected
521+
const testColor = 'blue';
522+
const expectedTemplate = `
523+
.test-global {
524+
color: ${testColor};
525+
}
526+
`;
527+
expect(expectedTemplate).toContain('color: blue');
528+
expect(expectedTemplate).toContain('.test-global');
529+
});
530+
});
503531
});

src/tasty/injector/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ export function createInjector(
195195
return new StyleInjector(fullConfig);
196196
}
197197

198+
/**
199+
* Create a global style component like styled-components createGlobalStyle
200+
* Returns a React component that injects global styles when mounted and cleans up when unmounted
201+
*/
202+
export function createGlobalStyle<Props = {}>(
203+
strings: TemplateStringsArray,
204+
...interpolations: Array<
205+
string | number | ((props: Props) => string | number)
206+
>
207+
): import('react').ComponentType<Props & { root?: Document | ShadowRoot }> {
208+
return getGlobalInjector().createGlobalStyle(strings, ...interpolations);
209+
}
210+
198211
// Re-export types
199212
export type {
200213
StyleInjectorConfig,

0 commit comments

Comments
 (0)