Skip to content
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

Allow build process to split css files for front end and backend. #1418

Merged
merged 5 commits into from
Jun 29, 2017

Conversation

BE-Webdesign
Copy link
Contributor

Related to #963.

This PR splits up some of our CSS build process to have seperate files
generated for blocks and editor specific styles of blocks. Block
specific styles are now loaded both on the front-end and back end.
Styles that should appear on both are currently in a block.scss file.

Styles added:

wp-edit-blocks: blocks/build/edit-block.css
wp-blocks: blocks/build/style.css

Caveats:

It was suggested that the reverse be done, by using an edit.scss to hold
editor specific styles. This seemed pretty daunting a task to me and I
did not fully grasp what benefit that would bring over this approach
either.

** Testing Instructions **
build the files via npm run build or npm run dev

Verify that the gallery columns work both on the front end as well as
back end.

Verify that the drop cap style still works on the back end. It can't work yet on the front end because the styles are not carried over: #1417

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Great work. I think we're really close. I suggest adding guidelines to the styles included in block.scss

  • Always add the block generated class as a root level
  • Maybe limit to the block library folder

@@ -0,0 +1,16 @@
.drop-cap-true .blocks-editable__tinymce:not( :focus ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I was kind of expecting us to only provide block stylesheets in the frontend (block.scss only inside the library folder), but I may be wrong.

I think we should do:

  • Moving the drop cap style to the text block styles because it's an option specific to the text block.
  • .blocks-editable__tinymce we should drop this className from the styling because it's an "edit" className only.
  • I don't think we should style a global drop-cap-true className but more something like wp-block-text.is-drop-cap-enabled (We should probably always enforce the generated block className as a root of these styles shared with the frontend.

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, we can probably move this and remove the tinymce stuff. I though it was somewhat odd that the drop cap was in editable and not in text. I had to do an ack to find where the drop cap was. I think the idea behind it though is that there could be other fields that want a drop cap when using editable?

I am also not sure whether we want to change the styles around too much in this PR, and instead have this PR be focused more around the actual piping of styles in our webpack configuration. Then in subsequent PRs we can figure out where the styles should go. I know @jasmussen would have good ideas on how to break things up and configure them. If we start changing the styles now, we would have to change things on the text block as well, because currently the drop cap class names are not even added. We can still do it in this PR, but I would like to avoid merge conflicts 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we can probably move this and remove the tinymce stuff. I though it was somewhat odd that the drop cap was in editable and not in text. I had to do an ack to find where the drop cap was. I think the idea behind it though is that there could be other fields that want a drop cap when using editable?

In the backend, if the dropcap CSS is applied to the editable, deleting characters behaves really weirdly. I can't recall exactly what went wrong, but we solved it by deciding that the dropcap is applied only in the backend when your cursor isn't actually on the editable block. We'd obv. want it there always on the frontend.

Copy link
Member

Choose a reason for hiding this comment

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

@youknowriad yes, that was the original plan, a has-drop-cap class added to p.

@@ -0,0 +1,39 @@

.blocks-gallery {
Copy link
Contributor

Choose a reason for hiding this comment

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

Aside and could be done separately, but we should probably drop this className in favour of the generated one wp-block-gallery

Copy link
Contributor Author

@BE-Webdesign BE-Webdesign Jun 24, 2017

Choose a reason for hiding this comment

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

Yeah, that sounds like a good idea, same apprehension from the above comment applies here, as I am not sure how much we want to change the styles in this PR.

Copy link
Contributor Author

@BE-Webdesign BE-Webdesign Jun 26, 2017

Choose a reason for hiding this comment

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

If we do this, it will break on the editor side of things. Going to keep as .blocks-gallery for now.

Copy link
Member

Choose a reason for hiding this comment

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

@youknowriad let's figure out how to get these classes on the edit side of things.

Copy link
Member

Choose a reason for hiding this comment

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

We have these on edit now, so let's try to switch the class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Forgot about that. Thank you for the reminder. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like it is wp-block-gallery now.

@jasmussen
Copy link
Contributor

Nice work. Works as expected. A few quick questions: we will be compiling all core blocks to have front-end styles that load in a single stylesheet on the frontend, correct? I.e. styles for dropcaps, galleries, cover images etc. those all show up in a single stylesheet?

Also, can we remove comments from the compiled stylesheets? Or is there at least a way to limit them? Right now we have some CSS comments for the responsive variables, that show up multiple times in the generated CSS for some reason.

Can you talk a little bit more about how to style a block so that it works in both the editor and the front-end? This will also help for the documentation we'll want to write. Take for example the simplest block, the separator. It currently only has the following CSS in the backend:

.blocks-separator {
	border: none;
	border-bottom: 2px solid $dark-gray-100;
	max-width: 100px;
	margin: 1em auto;
}

Aside from the fact that we may not want to style the HR at all (and let themes do that), let's imagine that we did want to style it, what would the style.scss then look like for the block? For example we'd probably always want to try and avoid assigning colors, and leave that totally to the theme.

Note, asking not as critique of this PR, I think it's probably good to go given Riads changes, just trying to wrap my head around it. Thanks for working on this!

@youknowriad
Copy link
Contributor

Yes, the extracting mechanism and loading the style for frontend is good for me. As I said maybe I'd limit it to the "library" folder instead of all the blocks module.

I'm ok with merging and working on the stylesheets separately (we need guidelines on how separate edit and common styles effectively).

@BE-Webdesign
Copy link
Contributor Author

Note, asking not as critique of this PR

Critique is welcome.

just trying to wrap my head around it

Yeah, same lol. Not totally sure how this should be done, but the approach I took will split out any css in our blocks directory into two separate css files. Any block.scss files will be built into blocks/build/style.css, which is registered in WordPress as wp-blocks. Then any of the remaining .scss files in the blocks directory will be built into blocks/build/edit-block.css registered as wp-edit-blocks in WP. The rest of the build process is untouched. This is just one way you can enqueue the block styles in the editor and on the front end of a theme.

Also, can we remove comments from the compiled stylesheets? Or is there at least a way to limit them? Right now we have some CSS comments for the responsive variables, that show up multiple times in the generated CSS for some reason.

Yeah, going to open a separate issue as this already happens on our styles and makes them a lot bigger than they should be.

Aside from the fact that we may not want to style the HR at all (and let themes do that), let's imagine that we did want to style it, what would the style.scss then look like for the block?

No idea, the HR styles seemed admin specific, but we can port them over. Maybe it is fine to have these be the default styles for the front end even with our colors. In #1420, people will be able to override the default styles in their theme, so maybe we can move a lot of stuff to be shown in the theme and expect people to override them. A lot of people seem confused at how Gutenberg does not match the front end, so it could be good to maybe go crazy and bring a lot of stuff to the front end to just see what happens and how people respond.

@BE-Webdesign
Copy link
Contributor Author

Yes, the extracting mechanism and loading the style for frontend is good for me. As I said maybe I'd limit it to the "library" folder instead of all the blocks module.

Yup, I think I better understand what you were saying about the drop cap now. Instead of changing that stuff in the editable folder add the styles to the text block in the library?

const BlocksCSSPlugin = new ExtractTextPlugin( {
filename: './blocks/build/style.css',
} );

Copy link
Member

Choose a reason for hiding this comment

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

These vars should be lower-cased.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good.

exclude: [
/blocks/,
],
use: MainCSSExtractTextPlugin.extract( {
Copy link
Member

Choose a reason for hiding this comment

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

There is a lot of duplication here. Can we split this logic out into a function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

@nylen
Copy link
Member

nylen commented Jun 26, 2017

I think anything we do in this area should be designed to work with plugins that will need to register blocks + styles as well as with our own blocks. Otherwise, we are just going to do this work twice.

This would mean that we first build stylesheets for each "core" block separately, then in this PR or a separate one, register each block on the server along with its stylesheets.

@youknowriad
Copy link
Contributor

@nylen People are asking for frontend support and I think it could be great to ship this in our weekly releases while we polish the API. (unless we could get the API quickly, but the consensus seems harder to find there)

@nylen
Copy link
Member

nylen commented Jun 26, 2017

I'm not saying that we need to ship a perfect and fully-formed API for this on the first try, just that we need to put some thought into our known use-cases before we ship this.

This would mean that we first build stylesheets for each "core" block separately, then in this PR or a separate one, register each block on the server along with its stylesheets.

Thinking about it some more, this is probably overkill for not much benefit, and registering a block stylesheet + an editor stylesheet per plugin is probably sufficient, as well as easier to build.

@BE-Webdesign
Copy link
Contributor Author

Updated, waiting on GitHub to pull in changes. The update/split-editor-block-styles branch has the updated files.

@BE-Webdesign BE-Webdesign force-pushed the update/split-editor-block-styles branch from 19e54d5 to a55a004 Compare June 26, 2017 18:18
}

return <p style={ { textAlign: align } }>{ content }</p>;
}
Copy link
Contributor

@youknowriad youknowriad Jun 26, 2017

Choose a reason for hiding this comment

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

We could probably simplify the logic here to avoid duplication

const className = dropCap ? 'has-drop-cap' : undefined;

And always add the className prop. (undefined is equivalent to unset)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would definitely be a lot better.

@@ -85,13 +86,14 @@ registerBlockType( 'core/text', {
},

save( { attributes } ) {
const { align, content } = attributes;
const { align, content, dropCap } = attributes;
const className = dropCap ? 'has-drop-cap' : undefined;
Copy link
Member

Choose a reason for hiding this comment

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

This could simply be const className = dropCap && 'has-drop-cap';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good.

@@ -0,0 +1,16 @@
.has-drop-cap {
Copy link
Member

Choose a reason for hiding this comment

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

We should write this as p.has-drop-cap, since modifier classes should never be used without a main class or element as a target.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, sounds good.

@BE-Webdesign BE-Webdesign force-pushed the update/split-editor-block-styles branch from d3cc431 to cb68ea6 Compare June 28, 2017 21:36
@BE-Webdesign
Copy link
Contributor Author

Rebased, resolved conflicts, and made the latest recommended changes. Let me know if anything else needs a touch up.

Related to #963.

This PR splits up some of our CSS build process to have seperate files
generated for blocks and editor specific styles of blocks. Block
specific styles are now loaded both on the front-end and back end.
Styles that should appear on both are currently in a block.scss file.

Styles added:

wp-edit-blocks: blocks/build/edit-block.css
wp-blocks: blocks/build/style.css

Caveats:

It was suggested that the reverse be done, by using an edit.scss to hold
editor specific styles. This seemed pretty daunting a task to me and I
did not fully grasp what benefit that would bring over this approach
either.

** Testing Instructions **
build the files via `npm run build` or `npm run dev`

Verify that the gallery columns work both on the front end as well as
back end.

Verify that the drop cap style still works on the back end.
Adding documentation about the build process, updating variable names,
reducing duplication. Making the drop cap style work on the front end as
well as gallery styles.
Fixing test to not include empty class property.
Changing a ternary operator into an && statement. Adding a
p.has-drop-cap as the selector instead of just .has-drop-cap.
@youknowriad youknowriad force-pushed the update/split-editor-block-styles branch from cb68ea6 to 4b5f5fa Compare June 29, 2017 15:03
@youknowriad
Copy link
Contributor

Merging this, Let's get this in tomorrow's release. I'm going to work on splitting the other blocks' styles.

@BE-Webdesign
Copy link
Contributor Author

Should we restart Travis? Tests passed locally. Not sure why the build stalled.

@youknowriad youknowriad merged commit 8dfc4c5 into master Jun 29, 2017
@youknowriad youknowriad deleted the update/split-editor-block-styles branch June 29, 2017 15:41
@aduth
Copy link
Member

aduth commented Jul 3, 2017

Any block.scss files will be built into blocks/build/style.css, which is registered in WordPress as wp-blocks. Then any of the remaining .scss files in the blocks directory will be built into blocks/build/edit-block.css registered as wp-edit-blocks in WP.

So in practice, block.scss is the "base" styles for a block we expect to apply in all contexts, and style.scss are editor-specific additions? To me, the naming could do for better distinction. edit.scss (or editor.scss) was mentioned in caveats of original message. This seems more sensible to me to at least provide a hint to the difference between the two stylesheets. Right now it's quite unclear.

@BE-Webdesign
Copy link
Contributor Author

BE-Webdesign commented Jul 3, 2017

@aduth

I agree, the problem I encountered while working on this is that most of the style.scss styles are editor related styles, and we have multiple other style sheet names, which contain mostly editor related styles. So as good as the naming for edit.scss etc. is, the practicality of making those changes in one PR would be merge conflict prone. This PR on its own had merge conflicts, can't imagine what it would have been if everything was renamed and relocated at once. The way things are set up things, styles can be slowly migrated into edit.scss and block.scss.

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.

None yet

7 participants