-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[Try] Remove inline style attributes paragraph and button. #3669
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -23,3 +23,4 @@ import './video'; | |
import './audio'; | ||
import './reusable-block'; | ||
import './paragraph'; | ||
import './style'; |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { registerBlockType } from '../../api'; | ||
|
||
registerBlockType( 'core/style', { | ||
title: __( 'Style' ), | ||
|
||
icon: 'editor-code', | ||
|
||
category: 'hidden', | ||
|
||
attributes: { | ||
content: { | ||
type: 'string', | ||
source: 'html', | ||
}, | ||
}, | ||
|
||
isComputedBlock: true, | ||
|
||
supportHTML: true, | ||
|
||
edit( ) { | ||
return []; | ||
}, | ||
|
||
save( { attributes } ) { | ||
return `<style type="text/css">${ attributes.content }</style>\n`; | ||
}, | ||
} ); |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { | ||
kebabCase, | ||
reduce, | ||
} from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { hash, hashColor } from '@wordpress/utils'; | ||
|
||
export function generateClass( styleObject, prefix = '' ) { | ||
const hashString = hash( styleObject ); | ||
return hashString ? prefix + hashString : undefined; | ||
} | ||
|
||
export function generateStyle( styleObject, className ) { | ||
if ( ! styleObject ) { | ||
return undefined; | ||
} | ||
const styleString = reduce( styleObject, ( memo, value, property ) => ( | ||
value ? `${ memo }\n\t${ kebabCase( property ) }: ${ value };` : memo | ||
), '' ); | ||
if ( ! styleString ) { | ||
return undefined; | ||
} | ||
const useClass = className || generateClass( styleObject ); | ||
return { | ||
[ useClass ]: `.${ useClass } {${ styleString }\n}`, | ||
}; | ||
} | ||
|
||
export function generateClassBackgroundColor( color ) { | ||
return color ? `background-color-${ hashColor( color ) }` : undefined; | ||
} | ||
|
||
export function generateStyleBackgroundColor( color, className ) { | ||
return color ? generateStyle( { backgroundColor: color }, className || generateClassBackgroundColor( color ) ) : undefined; | ||
} | ||
|
||
export function generateClassColor( color ) { | ||
return color ? `color-${ hashColor( color ) }` : undefined; | ||
} | ||
|
||
export function generateStyleColor( color, className ) { | ||
return color ? generateStyle( { color }, className || generateClassColor( color ) ) : undefined; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import tinycolor from 'tinycolor2'; | ||
import hashString from 'hash-string'; | ||
import { | ||
reduce, | ||
some, | ||
} from 'lodash'; | ||
|
||
export function hash( collection ) { | ||
if ( ! some( collection ) ) { | ||
return undefined; | ||
} | ||
return hashString( | ||
reduce( collection, ( memo, value ) => memo + value, '' ) | ||
).toString( 36 ); | ||
} | ||
|
||
export function hashColor( color ) { | ||
const tinyColor = tinycolor( color ); | ||
const colorName = tinyColor.toName(); | ||
if ( colorName ) { | ||
return colorName; | ||
} | ||
return hash( { color } ); | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the user picks
white
as color andblack
as background-color for both button and paragraph? Shouldn't we share the same class names for both blocks? With the current implementation, we will create one combined class for all properties that paragraph has.In other words, if it would work also to have:
when user picks the colors specified for the theme.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also use in the paragraph the same approach as in the button. So classes can be reused in different block types, but for now, I intentionally used different approaches so we can see what is possible. The plan for colors from the theme and default Gutenberg palette is to use a set of classes this will only be used for colors outside the ones in the palette :)