Skip to content

Commit

Permalink
[Mobile] Create cover block with custom colour (without an image) (#2…
Browse files Browse the repository at this point in the history
…3870)

* Create cover block with custom color using the Color Picker
  • Loading branch information
Antonis Lilis authored Jul 24, 2020
1 parent df28c29 commit bd4a6b7
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ function MediaPlaceholder( props ) {
value = [],
children,
height,
backgroundColor,
hideContent,
} = props;

// use ref to keep media array current for callbacks during rerenders
Expand Down Expand Up @@ -170,12 +172,13 @@ function MediaPlaceholder( props ) {
[
emptyStateContainerStyle,
height && { height },
backgroundColor && { backgroundColor },
],
isAppender && appenderStyle,
] }
>
{ getMediaOptions() }
{ renderContent() }
{ ! hideContent && renderContent() }
</View>
</TouchableWithoutFeedback>
);
Expand Down
73 changes: 68 additions & 5 deletions packages/block-library/src/cover/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import { View, TouchableWithoutFeedback } from 'react-native';
import Video from 'react-native-video';
import { noop } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -27,6 +26,8 @@ import {
ToolbarGroup,
Gradient,
ColorPalette,
ColorPicker,
BottomSheetConsumer,
} from '@wordpress/components';
import {
BlockControls,
Expand Down Expand Up @@ -81,6 +82,7 @@ const Cover = ( {
onFocus,
overlayColor,
setAttributes,
openGeneralSidebar,
settings,
closeSettingsBottomSheet,
} ) => {
Expand Down Expand Up @@ -112,6 +114,13 @@ const Cover = ( {
gradientValue
);

const [
isCustomColorPickerShowing,
setCustomColorPickerShowing,
] = useState( false );

const [ customColor, setCustomColor ] = useState( '' );

const openMediaOptionsRef = useRef();

// Used to set a default color for its InnerBlocks
Expand Down Expand Up @@ -191,6 +200,13 @@ const Cover = ( {
} );
}

function openColorPicker() {
if ( isParentSelected ) {
openGeneralSidebar();
setCustomColorPickerShowing( true );
}
}

const backgroundColor = getStylesFromColorScheme(
styles.backgroundSolid,
styles.backgroundSolidDark
Expand Down Expand Up @@ -277,6 +293,43 @@ const Cover = ( {
</InspectorControls>
);

const colorPickerControls = (
<InspectorControls>
<BottomSheetConsumer>
{ ( {
shouldEnableBottomSheetScroll,
shouldDisableBottomSheetMaxHeight,
onCloseBottomSheet,
onHardwareButtonPress,
isBottomSheetContentScrolling,
} ) => (
<ColorPicker
shouldEnableBottomSheetScroll={
shouldEnableBottomSheetScroll
}
shouldDisableBottomSheetMaxHeight={
shouldDisableBottomSheetMaxHeight
}
setColor={ ( color ) => {
setCustomColor( color );
setColor( color );
} }
onNavigationBack={ closeSettingsBottomSheet }
onCloseBottomSheet={ onCloseBottomSheet }
onHardwareButtonPress={ onHardwareButtonPress }
onBottomSheetClosed={ () => {
setCustomColorPickerShowing( false );
} }
isBottomSheetContentScrolling={
isBottomSheetContentScrolling
}
bottomLabelText={ __( 'Select a color' ) }
/>
) }
</BottomSheetConsumer>
</InspectorControls>
);

const renderBackground = ( getMediaOptions ) => (
<TouchableWithoutFeedback
accessible={ ! isParentSelected }
Expand Down Expand Up @@ -354,11 +407,16 @@ const Cover = ( {
</TouchableWithoutFeedback>
);

if ( ! hasBackground ) {
if ( ! hasBackground || isCustomColorPickerShowing ) {
return (
<View>
{ isCustomColorPickerShowing && colorPickerControls }
<MediaPlaceholder
height={ styles.mediaPlaceholderEmptyStateContainer.height }
backgroundColor={ customColor }
hideContent={
customColor !== '' && customColor !== undefined
}
icon={ placeholderIcon }
labels={ {
title: __( 'Cover' ),
Expand All @@ -372,11 +430,14 @@ const Cover = ( {
customColorIndicatorStyles={
styles.paletteColorIndicator
}
customIndicatorWrapperStyles={
styles.paletteCustomIndicatorWrapper
}
setColor={ setColor }
onCustomPress={ noop }
onCustomPress={ openColorPicker }
defaultSettings={ coverDefaultPalette }
shouldShowCustomIndicatorOption={ false }
shouldShowCustomLabel={ false }
shouldShowCustomVerticalSeparator={ false }
/>
</View>
</MediaPlaceholder>
Expand Down Expand Up @@ -473,12 +534,14 @@ export default compose( [
};
} ),
withDispatch( ( dispatch ) => {
const { openGeneralSidebar } = dispatch( 'core/edit-post' );

return {
openGeneralSidebar: () => openGeneralSidebar( 'edit-post/block' ),
closeSettingsBottomSheet() {
dispatch( 'core/edit-post' ).closeGeneralSidebar();
},
};
} ),

withPreferredColorScheme,
] )( Cover );
11 changes: 9 additions & 2 deletions packages/block-library/src/cover/style.native.scss
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,15 @@
.paletteColorIndicator {
margin-top: 0;
margin-bottom: 0;
width: 32px;
height: 32px;
width: $grid-unit-40;
height: $grid-unit-40;
}

.paletteCustomIndicatorWrapper {
flex-direction: row;
align-items: baseline;
width: $grid-unit-40;
height: $grid-unit-40;
}

.mediaPlaceholderEmptyStateContainer {
Expand Down
6 changes: 5 additions & 1 deletion packages/components/src/color-indicator/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ function ColorIndicator( {
return (
<Gradient
gradientValue={ gradientValue }
style={ [ styles.circleOption, styles.absolute ] }
style={ [
styles.circleOption,
styles.absolute,
style,
] }
key={ gradientValue }
/>
);
Expand Down
15 changes: 12 additions & 3 deletions packages/components/src/color-palette/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ function ColorPalette( {
shouldEnableBottomSheetScroll,
shouldShowCustomIndicatorOption = true,
shouldShowCustomLabel = true,
shouldShowCustomVerticalSeparator = true,
customColorIndicatorStyles,
customIndicatorWrapperStyles,
} ) {
const customSwatchGradients = [
'linear-gradient(120deg, rgba(255,0,0,.8), 0%, rgba(255,255,255,1) 70.71%)',
Expand Down Expand Up @@ -188,6 +190,11 @@ function ColorPalette( {
styles.customTextDark
);

const customIndicatorWrapperStyle = [
styles.customIndicatorWrapper,
customIndicatorWrapperStyles,
];

return (
<ScrollView
contentContainerStyle={ styles.contentContainer }
Expand Down Expand Up @@ -237,17 +244,19 @@ function ColorPalette( {
} ) }
{ shouldShowCustomIndicator && (
<View
style={ styles.customIndicatorWrapper }
style={ customIndicatorWrapperStyle }
onLayout={ onCustomIndicatorLayout }
>
<View style={ verticalSeparatorStyle } />
{ shouldShowCustomVerticalSeparator && (
<View style={ verticalSeparatorStyle } />
) }
<TouchableWithoutFeedback
onPress={ onCustomPress }
accessibilityRole={ 'button' }
accessibilityState={ { selected: isSelectedCustom() } }
accessibilityHint={ accessibilityHint }
>
<View style={ styles.customIndicatorWrapper }>
<View style={ customIndicatorWrapperStyle }>
<ColorIndicator
withCustomPicker={ ! isGradientSegment }
color={ customIndicatorColor }
Expand Down
32 changes: 28 additions & 4 deletions packages/components/src/color-picker/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ function ColorPicker( {
isGradientColor,
onNavigationBack,
onCloseBottomSheet,
onBottomSheetClosed,
onHardwareButtonPress,
bottomLabelText,
} ) {
const isIOS = Platform.OS === 'ios';
const hitSlop = { top: 22, bottom: 22, left: 22, right: 22 };
Expand Down Expand Up @@ -55,6 +58,10 @@ function ColorPicker( {
styles.colorText,
styles.colorTextDark
);
const selectColorTextStyle = usePreferredColorSchemeStyle(
styles.selectColorText,
styles.selectColorTextDark
);
const footerStyle = usePreferredColorSchemeStyle(
styles.footer,
styles.footerDark
Expand Down Expand Up @@ -82,7 +89,15 @@ function ColorPicker( {
}
setColor( activeColor );
shouldDisableBottomSheetMaxHeight( false );
onCloseBottomSheet( () => setColor( savedColor ) );
onCloseBottomSheet( () => {
setColor( savedColor );
if ( onBottomSheetClosed ) {
onBottomSheetClosed();
}
} );
if ( onHardwareButtonPress ) {
onHardwareButtonPress( onButtonPress );
}
}, [] );

function onHuePickerChange( { hue: h } ) {
Expand All @@ -99,6 +114,9 @@ function ColorPicker( {
onCloseBottomSheet( null );
shouldDisableBottomSheetMaxHeight( true );
setColor( action === 'apply' ? currentColor : savedColor );
if ( onBottomSheetClosed ) {
onBottomSheetClosed();
}
}

return (
Expand Down Expand Up @@ -157,9 +175,15 @@ function ColorPicker( {
) }
</View>
</TouchableWithoutFeedback>
<Text style={ colorTextStyle } selectable>
{ currentColor.toUpperCase() }
</Text>
{ bottomLabelText ? (
<Text style={ selectColorTextStyle }>
{ bottomLabelText }
</Text>
) : (
<Text style={ colorTextStyle } selectable>
{ currentColor.toUpperCase() }
</Text>
) }
<TouchableWithoutFeedback
onPress={ () => onButtonPress( 'apply' ) }
hitSlop={ hitSlop }
Expand Down
11 changes: 11 additions & 0 deletions packages/components/src/color-picker/style.native.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@
color: $dark-primary;
}

.selectColorText {
text-align: center;
color: $gray;
font-size: 16px;
line-height: 24px;
}

.selectColorTextDark {
color: $dark-primary;
}

.picker {
padding: $grid-unit-20;
border-radius: $grid-unit-20 / 2;
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
} from '@wordpress/primitives';
export { default as ColorIndicator } from './color-indicator';
export { default as ColorPalette } from './color-palette';
export { default as ColorPicker } from './color-picker';
export { default as Dashicon } from './dashicon';
export { default as Dropdown } from './dropdown';
export { default as DropdownMenu } from './dropdown-menu';
Expand Down

0 comments on commit bd4a6b7

Please sign in to comment.