-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor Styles: Add a transform for button rules (#29229)
* Add a transform for button rules * Also rewrite selectors for inputs * added tests, used constant on replace function * refactored updateSelector to be more efficient * linting * revert changes to wrap transform * moved the code to a new transform * moved the code to a new transform * updated tests * linting * text changes on tests * more linting * update snapshots' Co-authored-by: Ben Dwyer <[email protected]>
- Loading branch information
1 parent
f673d51
commit 6ee2e34
Showing
5 changed files
with
96 additions
and
6 deletions.
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
46 changes: 46 additions & 0 deletions
46
packages/block-editor/src/utils/transform-styles/transforms/avoid-editor-components.js
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,46 @@ | ||
/** | ||
* This transform targets inputs and buttons and prevents css styles for the editor | ||
* from the theme to bleed into the editor's components. It is a bit hacky but | ||
* it is contained and prevents us from having to do make changes over multiple files. | ||
* | ||
* @constant string IS_BUTTON_TAG Regex to check if the selector is a button tag selector. | ||
* @constant string IS_INPUT_TAG Regex to check if the selector is an input tag selector. | ||
*/ | ||
const IS_BUTTON_TAG = /^(button).*$/; | ||
|
||
const IS_INPUT_TAG = /^(input).*$/; | ||
|
||
const avoidEditorComponents = ( ignore = [] ) => ( node ) => { | ||
const updateSelector = ( selector ) => { | ||
if ( ignore.includes( selector.trim() ) ) { | ||
return selector; | ||
} | ||
|
||
if ( selector.match( IS_BUTTON_TAG ) ) { | ||
return selector.replace( | ||
IS_BUTTON_TAG, | ||
'button:not(.components-button):not([id^=mceu_])' | ||
); | ||
} | ||
|
||
if ( selector.match( IS_INPUT_TAG ) ) { | ||
return selector.replace( | ||
IS_INPUT_TAG, | ||
'input:not(.components-text-control__input):not(.components-placeholder__input):not(.components-form-token-field__input)' | ||
); | ||
} | ||
|
||
return selector; | ||
}; | ||
|
||
if ( node.type === 'rule' ) { | ||
return { | ||
...node, | ||
selectors: node.selectors.map( updateSelector ), | ||
}; | ||
} | ||
|
||
return node; | ||
}; | ||
|
||
export default avoidEditorComponents; |
13 changes: 13 additions & 0 deletions
13
.../src/utils/transform-styles/transforms/test/__snapshots__/avoid-editor-components.js.snap
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,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`CSS selector wrap should add editor classes to buttons 1`] = ` | ||
"button:not(.components-button):not([id^=mceu_]) { | ||
background-color: #ff0000; | ||
}" | ||
`; | ||
|
||
exports[`CSS selector wrap should add editor classes to inputs 1`] = ` | ||
"input:not(.components-text-control__input):not(.components-placeholder__input):not(.components-form-token-field__input) { | ||
border-color: #ff0000; | ||
}" | ||
`; |
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
29 changes: 29 additions & 0 deletions
29
packages/block-editor/src/utils/transform-styles/transforms/test/avoid-editor-components.js
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,29 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import traverse from '../../traverse'; | ||
import avoidEditorComponents from '../avoid-editor-components'; | ||
|
||
describe( 'CSS selector wrap', () => { | ||
it( 'should add editor classes to buttons', () => { | ||
const callback = avoidEditorComponents(); | ||
const input = ` | ||
button { | ||
background-color: #ff0000; | ||
}`; | ||
const output = traverse( input, callback ); | ||
|
||
expect( output ).toMatchSnapshot(); | ||
} ); | ||
|
||
it( 'should add editor classes to inputs', () => { | ||
const callback = avoidEditorComponents(); | ||
const input = ` | ||
input { | ||
border-color: #ff0000; | ||
}`; | ||
const output = traverse( input, callback ); | ||
|
||
expect( output ).toMatchSnapshot(); | ||
} ); | ||
} ); |