Skip to content

Commit

Permalink
sometimes do not use useAttributeEditHandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Aug 20, 2024
1 parent 3e987d5 commit aacf30c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
14 changes: 4 additions & 10 deletions src/block-components/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import {
AdvancedSelectControl,
} from '~stackable/components'
import { i18n } from 'stackable'
import {
useAttributeEditHandlers,
useBlockAttributesContext,
} from '~stackable/hooks'
import { useBlockAttributesContext } from '~stackable/hooks'

/**
* WordPress dependencies
Expand All @@ -31,6 +28,7 @@ import { Icon as _Icon } from '../icon'
import { applyFilters } from '@wordpress/hooks'
import { useSelect } from '@wordpress/data'
import { useBlockEditContext } from '@wordpress/block-editor'
import { getAttributeNameFunc } from '~stackable/util'

export const Icon = props => (
<_Icon.InspectorControls
Expand Down Expand Up @@ -111,9 +109,7 @@ export const ColorsControls = props => {
attrNameTemplate = 'button%s',
} = props

const {
getAttrName,
} = useAttributeEditHandlers( attrNameTemplate )
const getAttrName = getAttributeNameFunc( attrNameTemplate )

const buttonBackgroundColorType = useBlockAttributesContext( attributes => {
return attributes[ getAttrName( 'backgroundColorType' ) ]
Expand Down Expand Up @@ -182,9 +178,7 @@ const SizeControls = props => {
attrNameTemplate = 'button%s',
} = props

const {
getAttrName,
} = useAttributeEditHandlers( attrNameTemplate )
const getAttrName = getAttributeNameFunc( attrNameTemplate )

return ( <>
{ props.hasFullWidth && (
Expand Down
6 changes: 3 additions & 3 deletions src/block-components/helpers/size/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useAttributeEditHandlers, useDeviceType } from '~stackable/hooks'
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n'
import { getAttributeNameFunc } from '~stackable/util'

const Layout = props => {
const deviceType = useDeviceType()
Expand Down Expand Up @@ -114,9 +115,8 @@ Layout.defaultProps = {
}

const Spacing = props => {
const {
getAttrName,
} = useAttributeEditHandlers( props.attrNameTemplate )
// Don't use the hook form so we don't rerender
const getAttrName = getAttributeNameFunc( props.attrNameTemplate )

const {
labelPaddings = __( 'Paddings', i18n ),
Expand Down
8 changes: 4 additions & 4 deletions src/block-components/separator/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
InspectorStyleControls,
} from '~stackable/components'
import {
useAttributeEditHandlers, useBlockAttributesContext, useBlockSetAttributesContext,
useBlockAttributesContext,
useBlockSetAttributesContext,
} from '~stackable/hooks'

/*
Expand All @@ -26,6 +27,7 @@ import { i18n, showProNotice } from 'stackable'
import { __ } from '@wordpress/i18n'
import { applyFilters } from '@wordpress/hooks'
import { useMemo } from '@wordpress/element'
import { getAttributeNameFunc } from '~stackable/util'

const SEPARATOR_SHADOWS = [
'none',
Expand All @@ -47,9 +49,7 @@ const SeparatorControls = props => {
hasFlipVertically,
} = props

const {
getAttrName,
} = useAttributeEditHandlers( attrNameTemplate )
const getAttrName = getAttributeNameFunc( attrNameTemplate )

const separatorShadowOptions = applyFilters( 'stackable.separator.shadows', SEPARATOR_SHADOWS )

Expand Down
8 changes: 8 additions & 0 deletions src/util/attributes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ export const getAttributeName = ( attrName, deviceType = 'desktop', hoverState =
return `${ attrName }${ deviceAttrName }${ hoverAttrName }`
}

// This can be used instead of using a hook to prevent rerenderes:
// const { getAttrName } = useAttributeEditHandlers( attrNameTemplate )
// Now:
// const getAttrName = getAttrNameFunc( attrNameTemplate )
export const getAttributeNameFunc = attrNameTemplate => ( attrName, device = 'desktop', state = 'normal' ) => {
return getAttributeName( getAttrNameFunction( attrNameTemplate )( attrName ), device, state )
}

/**
* Checks whether an attribute is empty or is considered as an unset block
* attribute. Handles top, left, right and bottom default attribute.
Expand Down

0 comments on commit aacf30c

Please sign in to comment.