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

Cover Image: Add range slider for background dim #2815

Merged
merged 3 commits into from
Oct 2, 2017

Conversation

mcsf
Copy link
Contributor

@mcsf mcsf commented Sep 27, 2017

Implements #2006

gutenberg-cover-dim-slider-2

(previous screencast)

In terms of design:

  • Should the slider only appear when dimming is toggled? (Current)
  • Should the slider's position or other style reflect is subordinate relationship with the toggle?
  • Should the slider altogether replace the toggle?
  • Should I start this over? :)

In technical terms:

  • The toggle-based dimming was implemented with a CSS rule on a :before element. These aren't programmatically manipulable, so fully dynamic dimming (i.e., setting background for any alpha value) isn't possible. The current solution is based on ten hardcoded steps offered to the user.
  • Is it worth moving away from this? (My gut says no.)

@mcsf mcsf added [Feature] Blocks Overall functionality of blocks Needs Design Feedback Needs general design feedback. [Type] Enhancement A suggestion for improvement. labels Sep 27, 2017
@mcsf mcsf self-assigned this Sep 27, 2017
@mcsf mcsf requested review from aduth and melchoyce September 27, 2017 17:39
@codecov
Copy link

codecov bot commented Sep 27, 2017

Codecov Report

Merging #2815 into master will increase coverage by 0.2%.
The diff coverage is 50%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #2815     +/-   ##
=========================================
+ Coverage   33.77%   33.97%   +0.2%     
=========================================
  Files         191      191             
  Lines        5691     6199    +508     
  Branches      996     1136    +140     
=========================================
+ Hits         1922     2106    +184     
- Misses       3189     3421    +232     
- Partials      580      672     +92
Impacted Files Coverage Δ
blocks/library/cover-image/index.js 33.33% <50%> (+2.89%) ⬆️
blocks/library/code/index.js 53.84% <0%> (-3.3%) ⬇️
blocks/editable/index.js 8.92% <0%> (-1.59%) ⬇️
blocks/library/separator/index.js 60% <0%> (ø) ⬆️
editor/modes/visual-editor/block.js 0% <0%> (ø) ⬆️
editor/modes/visual-editor/block-list.js 0% <0%> (ø) ⬆️
blocks/library/preformatted/index.js 50% <0%> (ø) ⬆️
editor/store-persist.js 100% <0%> (ø) ⬆️
...ditor/modes/visual-editor/invalid-block-warning.js 0% <0%> (ø) ⬆️
blocks/library/video/index.js 21.21% <0%> (+0.15%) ⬆️
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e7f1223...7280973. Read the comment docs.

@melchoyce
Copy link
Contributor

In my opinion...

Should the slider altogether replace the toggle?

Yes, which answers your first two questions. I think we should get rid of the toggle and just show the slider, defaulting to 0. Then, I'd update the label to either "Dimness" or "Opacity" and show the value as a percentage of 100%, rather than 0–1.

@mcsf
Copy link
Contributor Author

mcsf commented Sep 27, 2017

All those sound good!

@mcsf
Copy link
Contributor Author

mcsf commented Sep 27, 2017

Updated:

gutenberg-cover-dim-slider-2

@aduth
Copy link
Member

aduth commented Sep 28, 2017

Noting that it's an inevitably as we iterate on blocks, but this will likely cause many existing Cover Image blocks to become flagged as invalid, due to markup / class name changes.

Related: #2541


function dimRatioToClass( ratio ) {
let rounded = Math.round( ratio / 10 );
// String#padStart not ready for prime time
Copy link
Member

Choose a reason for hiding this comment

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

This a difficult task to implement correctly, and I think the only viable option is to call out to a service which is designed to take on the burden of maintaining this implementation: http://left-pad.io/ :trollface:

On a more serious note, there's: https://lodash.com/docs/4.17.4#padStart

Or my personal preference, the negative slice:

( '0' + rounded ).slice( -2 );

Copy link
Contributor Author

@mcsf mcsf Sep 28, 2017

Choose a reason for hiding this comment

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

there's lodash

Ooh, Dash must have failed me. :) Though I like the negative slice better.

if ( rounded < 10 ) {
rounded = '0' + rounded;
}
return 'has-background-dim--' + rounded;
Copy link
Member

Choose a reason for hiding this comment

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

What was the reasoning behind the double dash? Could be misleading as a pattern that we don't encourage. Should be sufficient with the single dash.

Copy link
Contributor Author

@mcsf mcsf Sep 28, 2017

Choose a reason for hiding this comment

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

No reasoning, just reflexes. :)

background: rgba( 0,0,0,.9 );
}

&.has-background-dim.has-background-dim--10:before {
Copy link
Member

Choose a reason for hiding this comment

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

A SASS loop control could help avoid the duplication here, if maybe a bit overkill:

http://sass-lang.com/documentation/file.SASS_REFERENCE.html#_for
http://thesassway.com/intermediate/if-for-each-while#for

@for $i from 1 through 10 {
	&.has-background-dim.has-background-dim-#{ $i } {
		background-image: rgba( black, $i * 0.1 );
	}
}

(Which, I'm seeing could be an issue for the 0 prefixing, and begs the question: Do we need the padding?)

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, I'll drop the padding. I think my subconscious reasoning was that classes are strings, thus numbers injected into these should have the padding so that alphabetical sorting matches number sorting. ¯_(ツ)_/¯

const style = url
? { backgroundImage: `url(${ url })` }
: undefined;
const classes = classnames( className, {
'has-parallax': hasParallax,
'has-background-dim': hasBackgroundDim,
'has-background-dim': dimRatio !== 0,
[ dimRatioToClass( dimRatio ) ]: dimRatio !== 0,
Copy link
Member

Choose a reason for hiding this comment

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

Maybe dimRatioToClass should return an empty string or null value when it is not equal to zero, which we pass as a standalone argument to classnames to be dropped?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Saner, yes.

@mcsf mcsf force-pushed the add/cover-image-ranged-dimmer branch from cb56807 to e2d6697 Compare September 28, 2017 11:57
@mcsf
Copy link
Contributor Author

mcsf commented Sep 28, 2017

Thanks for the sanity, @aduth! Feedback addressed.

@mcsf mcsf force-pushed the add/cover-image-ranged-dimmer branch from e2d6697 to 89cd7ff Compare September 28, 2017 16:15
className,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== 0,
Copy link
Member

Choose a reason for hiding this comment

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

Was this added to try to preserve backwards compatibility with existing content? I'd have hoped it would be the case, but unfortunately existing cover image blocks appear to be flagged as invalid. The issue appears to be that the old content will have the has-background-dim class, but the dimRatio will not be found and therefore default to 0, thus causing a subsequent save to save without has-background-dim and cause content loss (the validator in action!). Maybe we should assign the default for dimRatio to mimic what had previously existed as the default for hasBackgroundDim effect?

(For debugging validation, see also new #2837)

Copy link
Contributor Author

@mcsf mcsf Sep 29, 2017

Choose a reason for hiding this comment

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

Was this added to try to preserve backwards compatibility with existing content?

Not really. For the issue of compatibility, I stopped to consider what we could do at the framework level. Regardless, something as small as this shouldn't be blocked by that, so I pushed 2884db eab53ef. Note, though, the weirdness of

 function dimRatioToClass( ratio ) {
-       return ratio === 0
+       return ( ratio === 0 || ratio === 50 )
                ? null
                : 'has-background-dim-' + Math.round( ratio / 10 );
 }

Since 50% dimming corresponds to the previous default value of hasBackgroundDim, we need to treat that specifically to avoid altering our block's class list.

Copy link
Member

@aduth aduth Sep 29, 2017

Choose a reason for hiding this comment

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

Not really.

If not, why do we need has-background-dim at all? (Edit: I say this, though it may allow us to keep compatibility)

Copy link
Member

Choose a reason for hiding this comment

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

Note, though, the weirdness of

I think this is fine, expected even. Similar to how we omit default values from serialized comments, we can omit the default value (50) from the class set.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So... my answer was for the past only. :)

I say this, though it may allow us to keep compatibility

Exactly. At first, it lingered while I pondered on the general compat stuff, but with that eab53ef it makes sense to keep it.

@mcsf mcsf force-pushed the add/cover-image-ranged-dimmer branch from 2884db5 to 31ebf85 Compare September 29, 2017 15:17
function dimRatioToClass( ratio ) {
return ( ratio === 0 || ratio === 50 )
? null
: 'has-background-dim-' + Math.round( ratio / 10 );
Copy link
Member

Choose a reason for hiding this comment

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

One more thing: Do we need the division? I might think .has-background-dim-50 is clearer at a glance than .has-background-dim-5

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The classes reflected the allowed granularity, but I can change that. Maybe it'll make it more future-proof.

Copy link
Contributor Author

@mcsf mcsf Sep 29, 2017

Choose a reason for hiding this comment

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

Actually, the division might still be needed, but the computation becomes:

10 * Math.round( ratio / 10 )

meaning that 35…44 -> 40, etc. Edit: in case a user inputs a specific value in between 40 and 50

@mcsf mcsf force-pushed the add/cover-image-ranged-dimmer branch from 31ebf85 to 7280973 Compare September 29, 2017 16:14
Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Confirmed back-compat, looks great 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Blocks Overall functionality of blocks Needs Design Feedback Needs general design feedback. [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants