Skip to content
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

React Native - Heading toolbar #9570

Merged
merged 9 commits into from Sep 8, 2018
1 change: 1 addition & 0 deletions packages/api-fetch/package.json
Expand Up @@ -19,6 +19,7 @@
},
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"dependencies": {
"@babel/runtime-corejs2": "7.0.0-beta.56",
"@wordpress/hooks": "file:../hooks",
Expand Down
25 changes: 24 additions & 1 deletion packages/block-library/src/heading/edit.native.js
Expand Up @@ -2,13 +2,15 @@
* External dependencies
*/
import { View } from 'react-native';
import { range } from 'lodash';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { RichText } from '@wordpress/editor';
import { Toolbar } from '@wordpress/components';

/**
* Internal dependencies
Expand All @@ -18,6 +20,26 @@ import './editor.scss';
const minHeight = 50;

class HeadingEdit extends Component {
createLevelControl( targetLevel ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract the implementation of createLevelControl into its own module file and re-use it both for the web and the native mobile? I think we should try that to avoid replicating code that is not specific to native mobile.

const {
attributes,
setAttributes,
} = this.props;

const {
level,
} = attributes;

return {
icon: 'heading',
// translators: %s: heading level e.g: "1", "2", "3"
title: sprintf( __( 'Heading %d' ), targetLevel ),
isActive: targetLevel === level,
onClick: () => setAttributes( { level: targetLevel } ),
subscript: String( targetLevel ),
};
}

render() {
const {
attributes,
Expand All @@ -33,6 +55,7 @@ class HeadingEdit extends Component {

return (
<View>
<Toolbar controls={ range( 2, 5 ).map( this.createLevelControl.bind( this ) ) } />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to https://github.com/WordPress/gutenberg/pull/9570/files#r214705073, it would be nice if we could avoid re-defining the toolbar buttons.

<RichText
tagName={ tagName }
content={ { contentTree: attributes.content, eventCount: attributes.eventCount } }
Expand Down
9 changes: 6 additions & 3 deletions packages/components/src/button/index.native.js
@@ -1,18 +1,21 @@
/**
* External dependencies
*/
import { TouchableOpacity } from 'react-native';
import { TouchableOpacity, Text, View } from 'react-native';

export default function Button( props ) {
const { children, onClick, 'aria-label': ariaLabel, 'aria-pressed': ariaPressed } = props;
const { children, onClick, 'aria-label': ariaLabel, 'aria-pressed': ariaPressed, 'data-subscript': subscript } = props;
return (
<TouchableOpacity
accessible={ true }
accessibilityLabel={ ariaLabel }
onPress={ onClick }
style={ { borderColor: ariaPressed ? 'black' : 'white', borderWidth: 1, borderRadius: 2 } }
>
{ children }
<View style={ { flex: 1, flexDirection: 'row' } }>
{ children }
{ subscript && ( <Text style={ { fontVariant: [ 'small-caps' ], textAlignVertical: 'bottom' } }>{ subscript }</Text> ) }
</View>
</TouchableOpacity>
);
}
Empty file.