Skip to content

Support for # id selectors #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,33 +167,33 @@ export default ({
},
JSXElement (path: Object, stats: Object): void {
const filename = stats.file.opts.filename;
const styleNameAttribute = path.node.openingElement.attributes
const styleAttribute = path.node.openingElement.attributes
.find((attribute) => {
return typeof attribute.name !== 'undefined' && attribute.name.name === 'styleName';
return typeof attribute.name !== 'undefined' && (attribute.name.name === 'styleName' || attribute.name.name === 'styleId');
});

if (!styleNameAttribute) {
if (!styleAttribute) {
return;
}

if (t.isStringLiteral(styleNameAttribute.value)) {
if (t.isStringLiteral(styleAttribute.value)) {
resolveStringLiteral(
path,
filenameMap[filename].styleModuleImportMap,
styleNameAttribute
styleAttribute
);

return;
}

if (t.isJSXExpressionContainer(styleNameAttribute.value)) {
if (t.isJSXExpressionContainer(styleAttribute.value)) {
if (!filenameMap[filename].importedHelperIndentifier) {
setupFileForRuntimeResolution(path, filename);
}
replaceJsxExpressionContainer(
t,
path,
styleNameAttribute,
styleAttribute,
filenameMap[filename].importedHelperIndentifier,
filenameMap[filename].styleModuleImportMapIdentifier
);
Expand Down
34 changes: 23 additions & 11 deletions src/replaceJsxExpressionContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,31 @@ import conditionalClassMerge from './conditionalClassMerge';
export default (
t: BabelTypes,
path: Object,
styleNameAttribute: JSXAttribute,
styleAttribute: JSXAttribute,
importedHelperIndentifier: Identifier,
styleModuleImportMapIdentifier: Identifier
): void => {
const expressionContainerValue = styleNameAttribute.value;
const expressionContainerValue = styleAttribute.value;

const styleNameExpression = t.callExpression(
importedHelperIndentifier,
[
expressionContainerValue.expression,
styleModuleImportMapIdentifier
]
);

if (styleAttribute.name.name === 'styleId') {
path.node.openingElement.attributes.push(jSXAttribute(
jSXIdentifier('id'),
jSXExpressionContainer(
styleNameExpression
)
));

return;
}

const classNameAttribute = path.node.openingElement.attributes
.find((attribute) => {
return typeof attribute.name !== 'undefined' && attribute.name.name === 'className';
Expand All @@ -28,15 +48,7 @@ export default (
path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(classNameAttribute), 1);
}

path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(styleNameAttribute), 1);

const styleNameExpression = t.callExpression(
importedHelperIndentifier,
[
expressionContainerValue.expression,
styleModuleImportMapIdentifier
]
);
path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(styleAttribute), 1);

if (classNameAttribute) {
if (isStringLiteral(classNameAttribute.value)) {
Expand Down
19 changes: 13 additions & 6 deletions src/resolveStringLiteral.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ import type {
/**
* Updates the className value of a JSX element using a provided styleName attribute.
*/
export default (path: Object, styleModuleImportMap: StyleModuleImportMapType, styleNameAttribute: JSXAttribute): void => {
export default (path: Object, styleModuleImportMap: StyleModuleImportMapType, styleAttribute: JSXAttribute): void => {
const resolvedStyleName = getClassName(styleAttribute.value.value, styleModuleImportMap);

if (styleAttribute.name.name === 'styleId') {
styleAttribute.name.name = 'id';
styleAttribute.value.value = resolvedStyleName;

return;
}

const classNameAttribute = path.node.openingElement.attributes
.find((attribute) => {
return typeof attribute.name !== 'undefined' && attribute.name.name === 'className';
});

const resolvedStyleName = getClassName(styleNameAttribute.value.value, styleModuleImportMap);

if (classNameAttribute) {
if (isStringLiteral(classNameAttribute.value)) {
classNameAttribute.value.value += ' ' + resolvedStyleName;
Expand All @@ -35,9 +42,9 @@ export default (path: Object, styleModuleImportMap: StyleModuleImportMapType, st
throw new Error('Unexpected attribute value.');
}

path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(styleNameAttribute), 1);
path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(styleAttribute), 1);
} else {
styleNameAttribute.name.name = 'className';
styleNameAttribute.value.value = resolvedStyleName;
styleAttribute.name.name = 'className';
styleAttribute.value.value = resolvedStyleName;
}
};