-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨 Adds support for namespacing keyframes and media queries
- Loading branch information
1 parent
d58e262
commit 4b8e5b3
Showing
7 changed files
with
362 additions
and
260 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,70 @@ | ||
type RuleSet = Record<string, Record<string, number | string>>; | ||
export type RuleSet = Record<string, Record<string, number | string>>; | ||
|
||
function namespace(id: string, style: Record<string, any>): RuleSet { | ||
const ruleSet: RuleSet = {}; | ||
|
||
(function pushRules(nestedId: string, nestedStyle: Record<string, any>) { | ||
Object.entries(nestedStyle).forEach(([property, value]) => { | ||
if (typeof value === 'string' || typeof value === 'number') { | ||
if (!ruleSet[nestedId]) ruleSet[nestedId] = {}; | ||
ruleSet[nestedId][property] = value; | ||
return; | ||
} | ||
function isObject(item: any) { | ||
return item && typeof item === 'object' && !Array.isArray(item); | ||
} | ||
|
||
let newRuleKey = property; | ||
function mergeDeep(target: any, ...sources: any): any { | ||
if (!sources.length) return target; | ||
const source = sources.shift(); | ||
|
||
if (property.includes('&')) { | ||
newRuleKey = property.replace(new RegExp(/&/, 'g'), nestedId); | ||
} else if (property.includes(':')) { | ||
newRuleKey = property.replace( | ||
new RegExp(/:/, 'g'), | ||
nestedId + ':', | ||
); | ||
if (isObject(target) && isObject(source)) { | ||
for (const key in source) { | ||
if (isObject(source[key])) { | ||
if (!target[key]) Object.assign(target, { [key]: {} }); | ||
mergeDeep(target[key], source[key]); | ||
} else { | ||
newRuleKey = property.replace( | ||
new RegExp(/,/, 'g'), | ||
', ' + nestedId, | ||
); | ||
newRuleKey = nestedId + ' ' + newRuleKey; | ||
Object.assign(target, { [key]: source[key] }); | ||
} | ||
} | ||
} | ||
|
||
return mergeDeep(target, ...sources); | ||
} | ||
|
||
function namespace(id: string, style: Record<string, any>): RuleSet { | ||
return Object.entries(style).reduce<RuleSet>((accum, [property, value]) => { | ||
if (typeof value !== 'object') { | ||
if (!accum[id]) accum[id] = {}; | ||
accum[id][property] = value; | ||
return accum; | ||
} | ||
|
||
if (property.includes('@keyframe')) { | ||
return mergeDeep(accum, { | ||
[property]: Object.entries(value).reduce<RuleSet>( | ||
( | ||
nestedAccum, | ||
[nestedProp, nestedValue]: [string, any], | ||
) => ({ | ||
...nestedAccum, | ||
...namespace(nestedProp, nestedValue), | ||
}), | ||
{}, | ||
), | ||
}); | ||
} else if (property.includes('@media')) { | ||
return mergeDeep(accum, { | ||
[property]: namespace(id, value), | ||
}); | ||
} | ||
|
||
let selector = ''; | ||
|
||
pushRules(`${newRuleKey}`, value); | ||
}); | ||
})(id, style); | ||
if (property.includes('&')) { | ||
selector = property.replace(new RegExp(/&/, 'g'), id); | ||
} else if (property.includes('::')) { | ||
selector = property.replace(new RegExp(/::/, 'g'), id + '::'); | ||
} else if (property.includes(':')) { | ||
selector = property.replace(new RegExp(/:/, 'g'), id + ':'); | ||
} else { | ||
selector = | ||
id + ' ' + property.replace(new RegExp(/,/, 'g'), ', ' + id); | ||
} | ||
|
||
return ruleSet; | ||
// TODO: deep merge is a shame | ||
return mergeDeep(accum, namespace(selector, value)); | ||
}, {}); | ||
} | ||
|
||
export default namespace; |
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
Oops, something went wrong.