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

Feat: Icon Library #3317

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/components/font-awesome-icon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ const addSVGAttributes = ( svgHTML, attributesToAdd = {}, attributesToRemove = [
}

const FontAwesomeIcon = memo( props => {
const {
svgAttrsToAdd = { width: '32', height: '32' },
svgAttrsToRemove = [ 'id', 'data-name' ],
} = props
const [ forceUpdateCount, setForceUpdateCount ] = useState( 0 )
const forceUpdate = () => {
setForceUpdateCount( forceUpdateCount + 1 )
Expand All @@ -91,7 +95,7 @@ const FontAwesomeIcon = memo( props => {
if ( typeof props.value === 'string' && props.value.match( /^<svg/ ) ) {
let svg = addSVGAriaLabel( props.value, props.ariaLabel )
// Add fallback SVG width and height values.
svg = addSVGAttributes( svg, { width: '32', height: '32' } )
svg = addSVGAttributes( svg, svgAttrsToAdd, svgAttrsToRemove )
return <RawHTML { ...propsToPass }>{ props.prependRenderString + svg }</RawHTML>
}

Expand All @@ -109,7 +113,7 @@ const FontAwesomeIcon = memo( props => {

let svg = addSVGAriaLabel( iconHTML, props.ariaLabel )
// Add fallback SVG width and height values.
svg = addSVGAttributes( svg, { width: '32', height: '32' } )
svg = addSVGAttributes( svg, svgAttrsToAdd, svgAttrsToRemove )
return <RawHTML { ...propsToPass }>{ props.prependRenderString + svg }</RawHTML>
}

Expand All @@ -123,7 +127,7 @@ const FontAwesomeIcon = memo( props => {

let svg = addSVGAriaLabel( iconHTML, props.ariaLabel )
// Add fallback SVG width and height values.
svg = addSVGAttributes( svg, { width: '32', height: '32' } )
svg = addSVGAttributes( svg, svgAttrsToAdd, svgAttrsToRemove )
return <RawHTML { ...propsToPass }>{ props.prependRenderString + svg }</RawHTML>
} )

Expand Down
31 changes: 26 additions & 5 deletions src/components/icon-search-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
PanelBody, TextControl, Spinner,
} from '@wordpress/components'
import { __ } from '@wordpress/i18n'
import { useState, useEffect } from '@wordpress/element'
import {
useState, useEffect, Fragment,
} from '@wordpress/element'

/**
* External dependencies
Expand All @@ -25,6 +27,7 @@ import {
import { faGetIcon, faFetchIcon } from '~stackable/util'
import { FileDrop } from 'react-file-drop'
import classnames from 'classnames'
import { applyFilters, doAction } from '@wordpress/hooks'

let searchTimeout = null
let tempMediaUpload = null
Expand Down Expand Up @@ -83,7 +86,7 @@ export const cleanSvgString = svgString => {

const IconSearchPopover = props => {
const [ value, setValue ] = useState( '' )
const [ results, setResults ] = useState( [] )
const [ results, setResults ] = useState( { faIcons: [], iconLibrary: [] } )
const [ isBusy, setIsBusy ] = useState( false )
const [ isDropping, setIsDropping ] = useState( false )

Expand Down Expand Up @@ -151,6 +154,9 @@ const IconSearchPopover = props => {
fr.onload = function( e ) {
setIsDropping( false )
const svgString = cleanSvgString( addCustomIconClass( e.target.result ) )

doAction( 'stackable.icon-search-popover.svg-upload', svgString )

props.onChange( svgString )
props.onClose()
}
Expand All @@ -168,6 +174,8 @@ const IconSearchPopover = props => {
'ugb-icon--has-reset': props.allowReset,
} )

const IconLibraryIcons = applyFilters( 'stackable.global-settings.inspector.icon-library.icons', Fragment )

const content = (
<div className="stk-icon-search-popover-container">
<FileDrop
Expand Down Expand Up @@ -244,13 +252,26 @@ const IconSearchPopover = props => {
</div>
<div className="ugb-icon-popover__iconlist">
{ isBusy && <Spinner /> }
{ ! isBusy && results.map( ( { prefix, iconName }, i ) => {
{ ! isBusy && <IconLibraryIcons
icons={ results.iconLibrary }
onChange={ props.onChange }
onClose={ props.onClose }
/> }
{ ! isBusy && results.faIcons.map( ( { prefix, iconName }, i ) => {
const iconValue = `${ prefix }-${ iconName }`
return <button
key={ i }
className={ `components-button ugb-prefix--${ prefix } ugb-icon--${ iconName }` }
onClick={ async () => {
if ( props.returnSVGValue ) {
if ( props.returnSVGValue && props.returnIconName ) {
let svgIcon = faGetIcon( prefix, iconName )

if ( ! svgIcon ) {
await faFetchIcon( prefix, iconName )
svgIcon = faGetIcon( prefix, iconName )
}
props.onChange( cleanSvgString( svgIcon ), prefix, iconName )
} else if ( props.returnSVGValue ) {
let svgIcon = faGetIcon( prefix, iconName )

if ( ! svgIcon ) {
Expand All @@ -267,7 +288,7 @@ const IconSearchPopover = props => {
<FontAwesomeIcon prefix={ prefix } iconName={ iconName } />
</button>
} ) }
{ ! isBusy && ! results.length &&
{ ! isBusy && ! results.faIcons.length && ! results.iconLibrary.length &&
<p className="components-base-control__help">{ __( 'No matches found', i18n ) }</p>
}
</div>
Expand Down
7 changes: 6 additions & 1 deletion src/components/icon-search-popover/search.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { applyFilters } from '@wordpress/hooks'
import {
fontAwesomeSearchProIcons, iconsFaKit, iconsFaProKitVersion, iconsFaFreeKitVersion,
} from 'stackable'
Expand All @@ -23,13 +24,17 @@ export const searchFontAwesomeIconName = async ( name = 'icon', isPro = fontAwes
} )
.then( r => r.json() )

return data.data.search.reduce( ( iconResults, iconData ) => {
const faIcons = data.data.search.reduce( ( iconResults, iconData ) => {
convertFontAwesomeToIcon( iconData, isPro ).forEach( icon => {
iconResults.push( icon )
} )

return iconResults
}, [] )

const iconLibrary = applyFilters( 'stackable.global-settings.inspector.icon-library.search-icons', null, name )

return { faIcons, iconLibrary }
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,4 @@ export { default as ColumnsWidthMultiControl } from './columns-width-multi-contr
export { default as Popover } from './popover'
export { default as HelpTooltip } from './help-tooltip'
export { default as RichText } from './rich-text'
export { default as SortablePicker } from './sortable-picker'
8 changes: 8 additions & 0 deletions src/components/pro-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ const LABELS = {
<li>{ __( 'Hide the current post - great for synced patterns', i18n ) }</li>
</ul>,
},
'icon-library': {
title: __( 'Unlock Your Icon Library', i18n ),
description: <ul>
<li>{ __( 'Add your custom SVG icons', i18n ) }</li>
<li>{ __( 'Easily access your custom icons in the icon picker', i18n ) }</li>
<li>{ __( 'Organize your custom icons in your library', i18n ) }</li>
</ul>,
},
}

const ProControl = props => {
Expand Down
Loading
Loading