Skip to content

Commit

Permalink
Add navigation to link settings and add link picker screen
Browse files Browse the repository at this point in the history
  • Loading branch information
dratwas committed Oct 16, 2020
1 parent 53d3f93 commit 4df464c
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
* WordPress dependencies
*/
import { InspectorControls } from '@wordpress/block-editor';
import { BottomSheet, ColorSettings } from '@wordpress/components';
import {
BottomSheet,
ColorSettings,
LinkPickerScreen,
} from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { withDispatch, withSelect } from '@wordpress/data';
/**
Expand All @@ -13,6 +17,7 @@ import styles from './container.native.scss';
export const blockSettingsScreens = {
settings: 'Settings',
color: 'Color',
linkPicker: 'linkPicker',
};

function BottomSheetSettings( {
Expand Down Expand Up @@ -41,6 +46,15 @@ function BottomSheetSettings( {
>
<ColorSettings defaultSettings={ settings } />
</BottomSheet.NavigationScreen>
<BottomSheet.NavigationScreen
name={ blockSettingsScreens.linkPicker }
fullScreen
isScrollable
>
<LinkPickerScreen
returnScreenName={ blockSettingsScreens.settings }
/>
</BottomSheet.NavigationScreen>
</BottomSheet.NavigationContainer>
</BottomSheet>
);
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/button/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
RangeControl,
ToolbarGroup,
ToolbarButton,
LinkSettings,
LinkSettingsNavigation,
} from '@wordpress/components';
import { Component } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -242,7 +242,7 @@ class ButtonEdit extends Component {
};

return (
<LinkSettings
<LinkSettingsNavigation
isVisible={ isLinkSheetVisible }
attributes={ attributes }
onClose={ this.dismissSheet }
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/social-link/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useEffect, useState, useRef } from '@wordpress/element';
import {
ToolbarGroup,
ToolbarButton,
LinkSettings,
LinkSettingsNavigation,
} from '@wordpress/components';
import { compose, usePreferredColorSchemeStyle } from '@wordpress/compose';
import { __, sprintf } from '@wordpress/i18n';
Expand Down Expand Up @@ -162,7 +162,7 @@ const SocialLinkEdit = ( {
</ToolbarGroup>
</BlockControls>
) }
<LinkSettings
<LinkSettingsNavigation
isVisible={ isLinkSheetVisible }
attributes={ attributes }
onEmptyURL={ onEmptyURL }
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export { default as CycleSelectControl } from './mobile/cycle-select-control';
export { default as Gradient } from './mobile/gradient';
export { default as ColorSettings } from './mobile/color-settings';
export { LinkPicker } from './mobile/link-picker';
export { default as LinkPickerScreen } from './mobile/link-picker/link-picker-screen';
export { default as LinkSettings } from './mobile/link-settings';
export { default as LinkSettingsScreen } from './mobile/link-settings/link-settings-screen';
export { default as LinkSettingsNavigation } from './mobile/link-settings/link-settings-navigation';
export { default as Image, IMAGE_DEFAULT_FOCAL_POINT } from './mobile/image';
export { default as ImageEditingButton } from './mobile/image/image-editing-button';
export { default as InserterButton } from './mobile/inserter-button';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import styles from './styles.scss';

const { placeholderColor } = styles;

export default function LinkCell( { value, onPress } ) {
export default function LinkCell( { value, onPress, showIcon = true } ) {
return (
<Cell
icon={ link }
icon={ showIcon && link }
label={ __( 'Link to' ) }
// since this is not actually editable, we treat value as a placeholder
value={ value || __( 'Search or type URL' ) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* External dependencies
*/
import React from 'react';
import { useNavigation, useRoute } from '@react-navigation/native';
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import { LinkPicker } from './';

const LinkPickerScreen = ( { returnScreenName } ) => {
const navigation = useNavigation();
const route = useRoute();

const onLinkPicked = ( { url, title } ) => {
navigation.navigate( returnScreenName, {
inputValue: url,
text: title,
} );
};

const onCancel = () => {
navigation.goBack();
};

const { inputValue } = route.params;
return useMemo( () => {
return (
<LinkPicker
value={ inputValue }
onLinkPicked={ onLinkPicked }
onCancel={ onCancel }
/>
);
}, [ inputValue ] );
};

export default LinkPickerScreen;
42 changes: 16 additions & 26 deletions packages/components/src/mobile/link-settings/index.native.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* External dependencies
*/
import { Platform, Clipboard } from 'react-native';
import { Clipboard } from 'react-native';
/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
import { isURL, prependHTTP } from '@wordpress/url';
import { useEffect, useState, useRef } from '@wordpress/element';
import { link, external } from '@wordpress/icons';
import { external } from '@wordpress/icons';

/**
* Internal dependencies
Expand Down Expand Up @@ -80,6 +80,8 @@ function LinkSettings( {
editorSidebarOpened,
// Specifies whether icon should be displayed next to the label
showIcon,
onLinkCellPressed,
urlValue,
} ) {
const { url, label, linkTarget, rel } = attributes;
const [ urlInputValue, setUrlInputValue ] = useState( '' );
Expand Down Expand Up @@ -115,12 +117,14 @@ function LinkSettings( {
}
}, [ editorSidebarOpened, isVisible ] );

function onChangeURL( value ) {
if ( ! value && onEmptyURL ) {
useEffect( () => {
if ( ! urlValue && onEmptyURL ) {
onEmptyURL();
}
setUrlInputValue( value );
}
setAttributes( {
url: prependHTTP( urlValue ),
} );
}, [ urlValue ] );

function onChangeLabel( value ) {
setLabelInputValue( value );
Expand Down Expand Up @@ -177,20 +181,10 @@ function LinkSettings( {
return (
<>
{ options.url && (
<TextControl
icon={ showIcon && link }
label={ options.url.label }
value={ urlInputValue }
valuePlaceholder={ options.url.placeholder }
onChange={ onChangeURL }
onSubmit={ onCloseSettingsSheet }
autoCapitalize="none"
autoCorrect={ false }
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus={
Platform.OS === 'ios' && options.url.autoFocus
}
keyboardType="url"
<BottomSheet.LinkCell
showIcon={ showIcon }
value={ url }
onPress={ onLinkCellPressed }
/>
) }
{ options.linkLabel && (
Expand Down Expand Up @@ -231,11 +225,7 @@ function LinkSettings( {
}

return (
<BottomSheet
isVisible={ isVisible }
onClose={ onCloseSettingsSheet }
hideHeader
>
<>
<PanelBody style={ styles.linkSettingsPanel }>
{ getSettings() }
</PanelBody>
Expand All @@ -248,7 +238,7 @@ function LinkSettings( {
</PanelBody>
) }
{ actions && <PanelActions actions={ actions } /> }
</BottomSheet>
</>
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Internal dependencies
*/
import BottomSheet from '../bottom-sheet';
import LinkSettingsScreen from './link-settings-screen';
import LinkPickerScreen from '../link-picker/link-picker-screen';

const linkSettingsScreens = {
settings: 'LinkSettingsScreen',
linkPicker: 'linkPicker',
};

function LinkSettingsNavigation( props ) {
if ( ! props.withBottomSheet ) {
return <LinkSettingsScreen { ...props } />;
}
return (
<BottomSheet
isVisible={ props.isVisible }
onClose={ props.onClose }
hideHeader
withNavigation
>
<BottomSheet.NavigationContainer animate main>
<BottomSheet.NavigationScreen
name={ linkSettingsScreens.settings }
>
<LinkSettingsScreen { ...props } withBottomSheet />
</BottomSheet.NavigationScreen>
<BottomSheet.NavigationScreen
name={ linkSettingsScreens.linkPicker }
isScrollable
fullScreen
>
<LinkPickerScreen
returnScreenName={ linkSettingsScreens.settings }
/>
</BottomSheet.NavigationScreen>
</BottomSheet.NavigationContainer>
</BottomSheet>
);
}

export default LinkSettingsNavigation;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* External dependencies
*/
import React from 'react';
import { useNavigation, useRoute } from '@react-navigation/native';
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
import LinkSettings from './';

const LinkSettingsScreen = ( props ) => {
const navigation = useNavigation();
const route = useRoute();
const { url = '' } = props.attributes || {};
const { inputValue = url } = route.params || {};

const onLinkCellPressed = () => {
navigation.navigate( 'linkPicker', { inputValue } );
};

return useMemo( () => {
return (
<LinkSettings
onLinkCellPressed={ onLinkCellPressed }
urlValue={ inputValue }
{ ...props }
/>
);
}, [ props, inputValue, navigation, route ] );
};

export default LinkSettingsScreen;

0 comments on commit 4df464c

Please sign in to comment.