-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block API: Add support for icons for block categories (#10651)
- Loading branch information
1 parent
e5b9987
commit 87732a2
Showing
11 changed files
with
319 additions
and
58 deletions.
There are no files selected for viewing
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
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,84 @@ | ||
# Icon | ||
|
||
Allows you to render a raw icon without any initial styling or wrappers. | ||
|
||
## Usage | ||
|
||
#### With a Dashicon | ||
|
||
```jsx | ||
import { Icon } from '@wordpress/components'; | ||
|
||
const MyIcon = () => ( | ||
<Icon icon="screenoptions" /> | ||
); | ||
``` | ||
|
||
#### With a function | ||
|
||
```jsx | ||
import { Icon } from '@wordpress/components'; | ||
|
||
const MyIcon = () => ( | ||
<Icon icon={ () => <svg><path d="M5 4v3h5.5v12h3V7H19V4z" /></svg> } /> | ||
); | ||
``` | ||
|
||
#### With a Component | ||
|
||
```jsx | ||
import { MyIconComponent } from '../my-icon-component'; | ||
import { Icon } from '@wordpress/components'; | ||
|
||
const MyIcon = () => ( | ||
<Icon icon={ MyIconComponent } /> | ||
); | ||
``` | ||
|
||
#### With an SVG | ||
|
||
```jsx | ||
import { Icon } from '@wordpress/components'; | ||
|
||
const MyIcon = () => ( | ||
<Icon icon={ <svg><path d="M5 4v3h5.5v12h3V7H19V4z" /></svg> } /> | ||
); | ||
``` | ||
|
||
#### Specifying a className | ||
|
||
```jsx | ||
import { Icon } from '@wordpress/components'; | ||
|
||
const MyIcon = () => ( | ||
<Icon icon="screenoptions" className="example-class" /> | ||
); | ||
``` | ||
|
||
## Props | ||
|
||
The component accepts the following props: | ||
|
||
### icon | ||
|
||
The icon to render. Supported values are: Dashicons (specified as strings), functions, WPComponent instances and `null`. | ||
|
||
- Type: `String|Function|WPComponent|null` | ||
- Required: No | ||
- Default: `null` | ||
|
||
### size | ||
|
||
The size (width and height) of the icon. | ||
|
||
- Type: `Number` | ||
- Required: No | ||
- Default: `20` when a Dashicon is rendered, `24` for all other icons. | ||
|
||
### className | ||
|
||
An optional additional class name to apply to the rendered icon. | ||
|
||
- Type: `String` | ||
- Required: No | ||
- Default: `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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { cloneElement, createElement, Component, isValidElement } from '@wordpress/element'; | ||
import { Dashicon, SVG } from '../'; | ||
|
||
function Icon( { icon = null, size, className } ) { | ||
let iconSize; | ||
|
||
if ( 'string' === typeof icon ) { | ||
// Dashicons should be 20x20 by default | ||
iconSize = size || 20; | ||
return <Dashicon icon={ icon } size={ iconSize } className={ className } />; | ||
} | ||
|
||
// Any other icons should be 24x24 by default | ||
iconSize = size || 24; | ||
|
||
if ( 'function' === typeof icon ) { | ||
if ( icon.prototype instanceof Component ) { | ||
return createElement( icon, { className, size: iconSize } ); | ||
} | ||
|
||
return icon(); | ||
} | ||
|
||
if ( icon && ( icon.type === 'svg' || icon.type === SVG ) ) { | ||
const appliedProps = { | ||
className, | ||
width: iconSize, | ||
height: iconSize, | ||
...icon.props, | ||
}; | ||
|
||
return <SVG { ...appliedProps } />; | ||
} | ||
|
||
if ( isValidElement( icon ) ) { | ||
return cloneElement( icon, { | ||
className, | ||
size: iconSize, | ||
} ); | ||
} | ||
|
||
return icon; | ||
} | ||
|
||
export default Icon; |
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,146 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { Path, SVG } from '../../'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Icon from '../'; | ||
|
||
describe( 'Icon', () => { | ||
const className = 'example-class'; | ||
const svg = <SVG><Path d="M5 4v3h5.5v12h3V7H19V4z" /></SVG>; | ||
|
||
it( 'renders nothing when icon omitted', () => { | ||
const wrapper = shallow( <Icon /> ); | ||
|
||
expect( wrapper.type() ).toBeNull(); | ||
} ); | ||
|
||
it( 'renders a dashicon by slug', () => { | ||
const wrapper = shallow( <Icon icon="format-image" /> ); | ||
|
||
expect( wrapper.find( 'Dashicon' ).prop( 'icon' ) ).toBe( 'format-image' ); | ||
} ); | ||
|
||
it( 'renders a dashicon and passes the classname to it', () => { | ||
const wrapper = shallow( <Icon icon="format-image" className={ className } /> ); | ||
|
||
expect( wrapper.find( 'Dashicon' ).prop( 'className' ) ).toBe( 'example-class' ); | ||
} ); | ||
|
||
it( 'renders a dashicon and with a default size of 20', () => { | ||
const wrapper = shallow( <Icon icon="format-image" /> ); | ||
|
||
expect( wrapper.find( 'Dashicon' ).prop( 'size' ) ).toBe( 20 ); | ||
} ); | ||
|
||
it( 'renders a dashicon and passes the size to it', () => { | ||
const wrapper = shallow( <Icon icon="format-image" size={ 32 } /> ); | ||
|
||
expect( wrapper.find( 'Dashicon' ).prop( 'size' ) ).toBe( 32 ); | ||
} ); | ||
|
||
it( 'renders a function', () => { | ||
const wrapper = shallow( <Icon icon={ () => <span /> } /> ); | ||
|
||
expect( wrapper.name() ).toBe( 'span' ); | ||
} ); | ||
|
||
it( 'renders an element', () => { | ||
const wrapper = shallow( <Icon icon={ <span /> } /> ); | ||
|
||
expect( wrapper.name() ).toBe( 'span' ); | ||
} ); | ||
|
||
it( 'renders an element and passes the classname to it', () => { | ||
const wrapper = shallow( <Icon icon={ <span /> } className={ className } /> ); | ||
|
||
expect( wrapper.prop( 'className' ) ).toBe( 'example-class' ); | ||
} ); | ||
|
||
it( 'renders an element and passes the size to it', () => { | ||
const wrapper = shallow( <Icon icon="format-image" size={ 32 } /> ); | ||
|
||
expect( wrapper.prop( 'size' ) ).toBe( 32 ); | ||
} ); | ||
|
||
it( 'renders an svg element', () => { | ||
const wrapper = shallow( <Icon icon={ svg } /> ); | ||
|
||
expect( wrapper.name() ).toBe( 'SVG' ); | ||
} ); | ||
|
||
it( 'renders an svg element and passes the classname to it', () => { | ||
const wrapper = shallow( <Icon icon={ svg } className={ className } /> ); | ||
|
||
expect( wrapper.prop( 'className' ) ).toBe( 'example-class' ); | ||
} ); | ||
|
||
it( 'renders an svg element with a default width and height of 24', () => { | ||
const wrapper = shallow( <Icon icon={ svg } /> ); | ||
|
||
expect( wrapper.prop( 'width' ) ).toBe( 24 ); | ||
expect( wrapper.prop( 'height' ) ).toBe( 24 ); | ||
} ); | ||
|
||
it( 'renders an svg element and passes the size as its width and height', () => { | ||
const wrapper = shallow( <Icon icon={ <SVG width={ 64 } height={ 64 }><Path d="M5 4v3h5.5v12h3V7H19V4z" /></SVG> } size={ 32 } /> ); | ||
|
||
expect( wrapper.prop( 'width' ) ).toBe( 64 ); | ||
expect( wrapper.prop( 'height' ) ).toBe( 64 ); | ||
} ); | ||
|
||
it( 'renders an svg element and does not override width and height if already specified', () => { | ||
const wrapper = shallow( <Icon icon={ svg } size={ 32 } /> ); | ||
|
||
expect( wrapper.prop( 'width' ) ).toBe( 32 ); | ||
expect( wrapper.prop( 'height' ) ).toBe( 32 ); | ||
} ); | ||
|
||
it( 'renders a component', () => { | ||
class MyComponent extends Component { | ||
render() { | ||
return <span />; | ||
} | ||
} | ||
const wrapper = shallow( | ||
<Icon icon={ MyComponent } /> | ||
); | ||
|
||
expect( wrapper.name() ).toBe( 'MyComponent' ); | ||
} ); | ||
|
||
it( 'renders a component and passes the classname to it', () => { | ||
class MyComponent extends Component { | ||
render( ) { | ||
return <span className={ this.props.className } />; | ||
} | ||
} | ||
const wrapper = shallow( | ||
<Icon icon={ MyComponent } className={ className } /> | ||
); | ||
|
||
expect( wrapper.prop( 'className' ) ).toBe( 'example-class' ); | ||
} ); | ||
|
||
it( 'renders a component and passes the size to it', () => { | ||
class MyComponent extends Component { | ||
render( ) { | ||
return <span size={ this.props.size } />; | ||
} | ||
} | ||
const wrapper = shallow( | ||
<Icon icon={ MyComponent } size={ 32 } /> | ||
); | ||
|
||
expect( wrapper.prop( 'size' ) ).toBe( 32 ); | ||
} ); | ||
} ); |
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
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.