-
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.
Add color hooks for the mobile implementation. (#21257)
* Add color hooks for the mobile implementation. * Update use-color for native * Null Line Controls for native. * Fix colors for Paragraph & Heading * fix media-text and cover * Merge branch 'master' into rnmobile/fix_color_breakage # Conflicts: # packages/block-library/src/heading/edit.js Co-authored-by: Pinar Olguc <[email protected]> Co-authored-by: Dratwas <[email protected]>
- Loading branch information
1 parent
69057a9
commit ddd3fff
Showing
13 changed files
with
227 additions
and
199 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
packages/block-editor/src/components/colors/color-panel.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,91 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { map } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PanelColorSettings from '../panel-color-settings'; | ||
import ContrastChecker from '../contrast-checker'; | ||
|
||
const resolveContrastCheckerColor = ( color, colorSettings, detectedColor ) => { | ||
if ( typeof color === 'function' ) { | ||
return color( colorSettings ); | ||
} else if ( color === true ) { | ||
return detectedColor; | ||
} | ||
return color; | ||
}; | ||
|
||
export default function ColorPanel( { | ||
title, | ||
colorSettings, | ||
colorPanelProps, | ||
contrastCheckers, | ||
detectedBackgroundColor, | ||
detectedColor, | ||
panelChildren, | ||
initialOpen, | ||
} ) { | ||
return ( | ||
<PanelColorSettings | ||
title={ title } | ||
initialOpen={ initialOpen } | ||
colorSettings={ Object.values( colorSettings ) } | ||
{ ...colorPanelProps } | ||
> | ||
{ contrastCheckers && | ||
( Array.isArray( contrastCheckers ) | ||
? contrastCheckers.map( | ||
( { backgroundColor, textColor, ...rest } ) => { | ||
backgroundColor = resolveContrastCheckerColor( | ||
backgroundColor, | ||
colorSettings, | ||
detectedBackgroundColor | ||
); | ||
textColor = resolveContrastCheckerColor( | ||
textColor, | ||
colorSettings, | ||
detectedColor | ||
); | ||
return ( | ||
<ContrastChecker | ||
key={ `${ backgroundColor }-${ textColor }` } | ||
backgroundColor={ backgroundColor } | ||
textColor={ textColor } | ||
{ ...rest } | ||
/> | ||
); | ||
} | ||
) | ||
: map( colorSettings, ( { value } ) => { | ||
let { | ||
backgroundColor, | ||
textColor, | ||
} = contrastCheckers; | ||
backgroundColor = resolveContrastCheckerColor( | ||
backgroundColor || value, | ||
colorSettings, | ||
detectedBackgroundColor | ||
); | ||
textColor = resolveContrastCheckerColor( | ||
textColor || value, | ||
colorSettings, | ||
detectedColor | ||
); | ||
return ( | ||
<ContrastChecker | ||
{ ...contrastCheckers } | ||
key={ `${ backgroundColor }-${ textColor }` } | ||
backgroundColor={ backgroundColor } | ||
textColor={ textColor } | ||
/> | ||
); | ||
} ) ) } | ||
{ typeof panelChildren === 'function' | ||
? panelChildren( colorSettings ) | ||
: panelChildren } | ||
</PanelColorSettings> | ||
); | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/block-editor/src/components/colors/color-panel.native.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,3 @@ | ||
export default function ColorPanel() { | ||
return null; | ||
} |
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
10 changes: 0 additions & 10 deletions
10
packages/block-editor/src/components/colors/use-colors.native.js
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useState, useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PanelColorSettings from '../components/panel-color-settings'; | ||
import ContrastChecker from '../components/contrast-checker'; | ||
import InspectorControls from '../components/inspector-controls'; | ||
import { getBlockDOMNode } from '../utils/dom'; | ||
|
||
export default function ColorPanel( { colorSettings, clientId } ) { | ||
const { getComputedStyle, Node } = window; | ||
|
||
const [ detectedBackgroundColor, setDetectedBackgroundColor ] = useState(); | ||
const [ detectedColor, setDetectedColor ] = useState(); | ||
|
||
useEffect( () => { | ||
const colorsDetectionElement = getBlockDOMNode( clientId ); | ||
setDetectedColor( getComputedStyle( colorsDetectionElement ).color ); | ||
|
||
let backgroundColorNode = colorsDetectionElement; | ||
let backgroundColor = getComputedStyle( backgroundColorNode ) | ||
.backgroundColor; | ||
while ( | ||
backgroundColor === 'rgba(0, 0, 0, 0)' && | ||
backgroundColorNode.parentNode && | ||
backgroundColorNode.parentNode.nodeType === Node.ELEMENT_NODE | ||
) { | ||
backgroundColorNode = backgroundColorNode.parentNode; | ||
backgroundColor = getComputedStyle( backgroundColorNode ) | ||
.backgroundColor; | ||
} | ||
|
||
setDetectedBackgroundColor( backgroundColor ); | ||
} ); | ||
|
||
return ( | ||
<InspectorControls> | ||
<PanelColorSettings | ||
title={ __( 'Color settings' ) } | ||
initialOpen={ false } | ||
colorSettings={ colorSettings } | ||
> | ||
<ContrastChecker | ||
backgroundColor={ detectedBackgroundColor } | ||
textColor={ detectedColor } | ||
/> | ||
</PanelColorSettings> | ||
</InspectorControls> | ||
); | ||
} |
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,3 @@ | ||
export default function ColorPanel() { | ||
return null; | ||
} |
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 |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
*/ | ||
import './custom-class-name'; | ||
import './generated-class-name'; | ||
import './style'; | ||
import './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
Oops, something went wrong.