Skip to content

feat(support-various-styling): add support for 3 styling types (closes #4)#9

Open
morajabi wants to merge 1 commit intomainfrom
support-various-styling
Open

feat(support-various-styling): add support for 3 styling types (closes #4)#9
morajabi wants to merge 1 commit intomainfrom
support-various-styling

Conversation

@morajabi
Copy link
Collaborator

@morajabi morajabi commented Sep 20, 2017

Hey, another one today!

This one was a bit more complicated, it needed some serious (🤔) structural changes to the codebase. I'm in the middle of a project but I really like this tool and started to do it! so I hope you like it. Most of it is based on your awesome idea (Thanks, @joshwcomeau ❤️) + some tweaks to optimize and future proof it.

I added 3 styling solutions for now (we can add more easily later!):

Styled-Components

Sample code:

// Hello.js
import React, { Component } from "react";
import styled from "styled-components";

class Hello extends Component {
  render() {
    return <Wrapper />;
  }
}

const Wrapper = styled.div``;

export default Hello;

Aphrodite

Sample code:

// HelloAph.js
import React, { Component } from "react";
import { StyleSheet, css } from "aphrodite";

class HelloAph extends Component {
  render() {
    return <div className={css(styles.wrapper)} />;
  }
}

const styles = StyleSheet.create({
  wrapper: {}
});

export default HelloAph;

CSS Modules

Sample code:

// HelloCSS.js
import React, { Component } from "react";
import styles from "./HelloCSS.css";

class HelloCSS extends Component {
  render() {
    return <div className={styles.wrapper} />;
  }
}

export default HelloCSS;

And

/* HelloCSS.css */
.wrapper {
}

.
.
Now I'm gonna use it everyday ✌️😋

@morajabi
Copy link
Collaborator Author

BTW, Sorry I was deep inside the code and forgot to commit features one at a time

@morajabi morajabi changed the title feat(support-various-styling): add support for 3 styling types feat(support-various-styling): add support for 3 styling types (closes #4) Sep 20, 2017
Copy link
Owner

@joshwcomeau joshwcomeau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omg this is awesome!!! Thanks so so much for your work on it, quality is fantastic. Both your PRs went above and beyond the requirements, so I really appreciate it :)

Don't worry about it all being 1 commit; I like to squash PRs before merging anyway, so it makes no difference to me.

I'm requesting changes for 1 main reason, and that's to add none as a valid styling type (where the templates would just do nothing, but support it as an option), where we still log it out.

I have a bunch of tiny nitpicks as well, but they should be really quick to implement.

I know you're busy so feel free to hand off this work to me, I can do the changes I requested. I'm in no rush, though, so if you want to wait and tackle it later, that's 100% cool with me as well.

Thanks Mohammad!

CSS_MODULES: 'css-modules',
APHRODITE: 'aphrodite',
};
module.exports.stylingTypes = stylingTypes;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think we can just do module.exports.componentTypes = { ... }. Same for stylingTypes.

My reasoning is that I'm so used to ES6 exports... the fact that it's const stylingTypes and not export const stylingTypes made me think it wasn't exported... so having it connected directly to module.exports = would make it easier to tell at a glance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning is that I'm so used to ES6 exports
Me too.

They were like what you said, but later I discovered we'll need these variables here for example. Can we reference these by modules.exports.componentTypes?

Copy link
Owner

@joshwcomeau joshwcomeau Oct 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, I see. Didn't realize that. Yeah, I'd leave this as-is then :)

EDIT: For what it's worth, you can reference them that way. but it feels a little funny to me.

@@ -66,17 +95,21 @@ const logComponentType = (selected) => (
)).join(' ')
);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's enough duplication that an abstraction could come in handy here :)

const logSelected = (values, selected) => (
  Object.values(values)
    .sort((a, b) => a === selected ? -1 : 1)
    .map(option => (
      option === selected
        ? `${chalk.bold.rgb(...colors.blue)(option)}`
        : `${chalk.rgb(...colors.darkGray)(option)}`
    )).join('  ')
);

Then you could just use it:

const componentTypeString = logSelected(componentTypes, type);
const stylingTypeString = logSelected(stylingTypes, styling);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good refactoring tip. I'll do better with refactoring later on.

${belowComponentTemplates(style)}

${exportsTemplates(style, componentName)}
`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: So with the templates (even the ones I wrote), we don't indent the template string contents:

const template = `
stuff
`;

Initially this was because I didn't want the exported file having a bunch of leading whitespace... but now that I think about it, I think prettier would trim all that, so it wouldn't be a problem.

In this case though, because it's already indented by the module.exports function, I think it's fine to indent:

module.exports = (componentName, componentType, style) => {
  const template = `
    stuff
  `;
}

No change necessary for the other template strings though (unless you think it'd be better; I'm happy either way with the stuff in templates/component-definition.js)

STYLED: 'styled-components',
CSS_MODULES: 'css-modules',
APHRODITE: 'aphrodite',
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should consider none a valid styling type.

My reasoning is that the default styling is no styling. Someone may install this project, skim the README too quickly, and not even realize that it's capable of doing styling!

By showing that "None" is the styling option they've selected, we also show the options they haven't selected. They might say "oh cool this does styled-components! I need that".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK! When you said you want to keep the project as simple as possible, I thought showing the styling: none styled css-modules aphrodite in console every time would confuse beginner users.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, my fault for not specifying! I can see the argument of wanting to reduce the noise as much as possible, but I think that ultimately it's worth the added line of info :)

console.info(`Directory: ${pathString}`);
console.info(`Type: ${typeString}`);
console.info(`Type: ${componentTypeString}`);
if (styling && styling !== 'none') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I talked more about this above, but yeah I think we should log the styling even when "none" is selected :)

logItemCompletion('Index file built and saved to disk.');
return template;
})
.then(template => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Didn't realize I was pointlessly passing the template down :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused for a few moments why we're doing this :))

const aphrodite = `\
const styles = StyleSheet.create({
wrapper: {
},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Same note as above; if possible with Prettier, can we add a blank line between open and close curlies?

Copy link
Collaborator Author

@morajabi morajabi Sep 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think as Prettier introduces itself as an opinionated code formatter, it shouldn't be possible :) (Also I couldn't find an option to avoid it here)

But maybe we can write a placeholder style for wrapper as create-react-app does that when you first init a project. I mean something like this:

{
  wrapper: {
    height: 'auto',
    width: 'auto',
  }
}

What do you think? Happy to discuss!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its ok :) no change necessary, I'd rather not apply any styles, and it's super easy for a developer to just add that new line themselves when they want to add styles.


const styled = `\
const Wrapper = styled.div\`
\`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can you add a blank line inside the template string?

Will Prettier strip it out, if you do? I like the idea of having a space for someone to add their styles, but it's not a big deal :)

Copy link
Collaborator Author

@morajabi morajabi Sep 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will Prettier strip it out, if you do?

Yeah, Prettier will make it like so:

const Wrapper = styled.div``

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok! No problem then :)

case componentTypes.PURE_CLASS:
return pureClass(...args);
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great method! Good use of rest/spread :)

@@ -0,0 +1,4 @@
module.exports = `
.wrapper {
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: same note as above, add a blank line if possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants