-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: decrease bundle size and remove extra deps
add injectUniqueKeyframe function + tests
- Loading branch information
Showing
14 changed files
with
140 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
import '@testing-library/jest-dom'; | ||
|
||
import {JSDOM} from 'jsdom'; | ||
const dom = new JSDOM(); | ||
global.document = dom.window.document; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const styleEl = document.createElement('style'); | ||
styleEl.setAttribute('data-testid', 'injectUniqueKeyframe'); | ||
document.head.appendChild(styleEl); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const styleSheet = styleEl.sheet!; | ||
|
||
export const injectUniqueKeyframe = (keyframe: string, name: string) => { | ||
// eslint-disable-next-line no-useless-escape | ||
const escapedName = name.replace(/[\(|\)|\,|\ |\#|\%]/g, ''); | ||
const animationName = `animation-${escapedName}`; | ||
const keyframes = ` | ||
@keyframes ${animationName} { | ||
${keyframe} | ||
}`; | ||
|
||
styleSheet?.insertRule(keyframes, styleSheet?.cssRules?.length); | ||
const uniqueRules: string[] = []; | ||
const uniqueIndexes: number[] = []; | ||
for (let index = 0; index < styleSheet?.cssRules?.length; index++) { | ||
if ( | ||
typeof styleSheet?.cssRules[index]?.cssText === 'string' && | ||
!uniqueRules.includes(styleSheet?.cssRules[index]?.cssText) | ||
) { | ||
uniqueRules.push(styleSheet?.cssRules[index]?.cssText); | ||
uniqueIndexes.push(index); | ||
} | ||
} | ||
for (let index = 0; index < styleSheet?.cssRules?.length; index++) { | ||
if (!uniqueIndexes.includes(index)) { | ||
styleSheet?.deleteRule(index); | ||
} | ||
} | ||
|
||
return {animationName, rulesLength: styleSheet?.cssRules?.length}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {injectUniqueKeyframe} from './cssInjector'; | ||
|
||
describe('injectUniqueKeyframe', () => { | ||
it('should set keyframes', () => { | ||
const firstRule = 'first-test'; | ||
const secondRule = 'second-rule'; | ||
const keyframe = `0% { | ||
transform: scale(0.5); | ||
box-shadow: 0 0 0 0 red; | ||
} | ||
100% { | ||
transform: scale(0.95); | ||
box-shadow: 0 0 0 0 rgba(0, 0, 0, 0); | ||
}`; | ||
|
||
const {rulesLength: firstAnimationNameRulesLength} = injectUniqueKeyframe(keyframe, firstRule); | ||
expect(firstAnimationNameRulesLength).toBe(1); | ||
|
||
const {rulesLength: secondAnimationNameRulesLength} = injectUniqueKeyframe(keyframe, secondRule); | ||
expect(secondAnimationNameRulesLength).toBe(2); | ||
|
||
const {rulesLength: thirdAnimationNameRulesLength} = injectUniqueKeyframe(keyframe, secondRule); | ||
expect(thirdAnimationNameRulesLength).toBe(2); | ||
|
||
const {rulesLength: fourthAnimationNameRulesLength} = injectUniqueKeyframe(keyframe, firstRule); | ||
expect(fourthAnimationNameRulesLength).toBe(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.