Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Switch to a button-click navigation pattern for better keyboard support
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle committed Jun 24, 2019
1 parent 7437519 commit 2e30298
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
31 changes: 25 additions & 6 deletions assets/js/blocks/product-categories/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import classnames from 'classnames';
import { Component, createRef, Fragment } from '@wordpress/element';
import { IconButton } from '@wordpress/components';
import { repeat } from 'lodash';
import PropTypes from 'prop-types';
import { withInstanceId } from '@wordpress/compose';
Expand All @@ -27,14 +29,18 @@ function getCategories( { hasEmpty, isHierarchical } ) {
class ProductCategoriesBlock extends Component {
constructor() {
super( ...arguments );
this.select = createRef();
this.onNavigate = this.onNavigate.bind( this );
this.renderList = this.renderList.bind( this );
this.renderOptions = this.renderOptions.bind( this );
}

onNavigate( event ) {
onNavigate() {
const { isPreview = false } = this.props;
const url = event.target.value;
const url = this.select.current.value;
if ( 'false' === url ) {
return;
}
const home = wc_product_block_data.homeUrl;

if ( ! isPreview && 0 === url.indexOf( home ) ) {
Expand Down Expand Up @@ -80,19 +86,32 @@ class ProductCategoriesBlock extends Component {
const { attributes, instanceId } = this.props;
const { isDropdown } = attributes;
const categories = getCategories( attributes );
const classes = classnames( {
'wc-block-product-categories': true,
'is-dropdown': isDropdown,
'is-list': ! isDropdown,
} );

const selectId = `prod-categories-${ instanceId }`;

return (
<div className="wc-block-product-categories">
<div className={ classes }>
{ isDropdown ? (
<Fragment>
<label htmlFor={ selectId }>
<label className="screen-reader-text" htmlFor={ selectId }>
{ __( 'Select a category', 'woo-gutenberg-products-block' ) }
</label>
<select id={ selectId } onChange={ this.onNavigate }>
<select id={ selectId } ref={ this.select }>
<option value="false">
{ __( 'Select a category', 'woo-gutenberg-products-block' ) }
</option>
{ this.renderOptions( categories ) }
</select>
<IconButton
icon="arrow-right-alt2"
label={ __( 'Go to category', 'woo-gutenberg-products-block' ) }
onClick={ this.onNavigate }
/>
</Fragment>
) : (
this.renderList( categories )
Expand Down
1 change: 1 addition & 0 deletions assets/js/blocks/product-categories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { registerBlockType } from '@wordpress/blocks';
* Internal dependencies
*/
import './editor.scss';
import './style.scss';
import edit from './edit.js';
import { IconFolder } from '../../components/icons';

Expand Down
11 changes: 11 additions & 0 deletions assets/js/blocks/product-categories/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.wc-block-product-categories {
margin-bottom: 1em;

&.is-dropdown {
display: flex;
}

select {
margin-right: 0.5em;
}
}
2 changes: 1 addition & 1 deletion assets/php/class-wgpb-block-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected static function register_style( $handle, $src, $deps = array(), $media
*/
public static function register_assets() {
self::register_style( 'wc-block-editor', plugins_url( 'build/editor.css', WGPB_PLUGIN_FILE ), array( 'wp-edit-blocks' ) );
self::register_style( 'wc-block-style', plugins_url( 'build/style.css', WGPB_PLUGIN_FILE ), array() );
self::register_style( 'wc-block-style', plugins_url( 'build/style.css', WGPB_PLUGIN_FILE ), array( 'wp-components' ) );

// Shared libraries and components across all blocks.
self::register_script( 'wc-blocks', plugins_url( 'build/blocks.js', WGPB_PLUGIN_FILE ), array(), false );
Expand Down

0 comments on commit 2e30298

Please sign in to comment.