-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* better interpolation * fixup
- Loading branch information
Showing
11 changed files
with
388 additions
and
486 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,41 @@ | ||
export default function wrapInClass(className, value) { | ||
import { stripIndents } from 'common-tags'; | ||
|
||
export default function wrapInClass(text) { | ||
const imports = []; | ||
const composes = []; | ||
|
||
let match; | ||
const matcher = /@import.*?(?:$|;)/g; | ||
const rImports = /@import.*?(?:$|;)/g; | ||
const rComposes = /\b(?:composes\s*?:\s*(.*?)(?:from\s(.+?))?(?=[}\n\r]))/gim; | ||
|
||
// eslint-disable-next-line no-cond-assign | ||
while ((match = matcher.exec(value))) { | ||
while ((match = rImports.exec(text))) { | ||
imports.push(match[0]); | ||
} | ||
match = null; | ||
// eslint-disable-next-line no-cond-assign | ||
while ((match = rComposes.exec(text))) { | ||
composes.push(match[0]); | ||
} | ||
|
||
value = value.replace(matcher, ''); | ||
text = text.replace(rImports, '').replace(rComposes, ''); | ||
|
||
// comment prevents Sass from removing the empty class | ||
let val = stripIndents` | ||
.cls1 { | ||
${text.trim() || '/*!*/'} | ||
}`; | ||
|
||
// Components need two css classes, the first being the single root | ||
// class with styles and the second is the exported name used in the Styled helper | ||
// We need both so that that interpolations have a class that is _only_ | ||
// the single class, e.g. no additional classes composed in so that it can be used | ||
// as a selecto | ||
if (composes.length) { | ||
composes.unshift(`composes: cls1;`); | ||
val += `\n.cls2 {\n${composes.join('\n')}\n}`; | ||
} | ||
|
||
let val = `${className} {\n${value}\n}`; | ||
if (imports.length) val = `${imports.join('\n')}\n${val}`; | ||
return val; | ||
} |
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.