Skip to content

Commit 31246d1

Browse files
committed
fix: differentiate Block and Link Custom Attributes
1 parent ef75d48 commit 31246d1

23 files changed

Lines changed: 97 additions & 70 deletions

File tree

‎src/block-components/block-link/index.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Edit } from './edit'
88
* External dependencies
99
*/
1010
import { Link } from '~stackable/components'
11+
import { CustomAttributes } from '../custom-attributes'
1112

1213
export const BlockLink = () => {
1314
return null
@@ -21,15 +22,16 @@ BlockLink.Content = props => {
2122
if ( ! attributes.blockLinkUrl ) {
2223
return null
2324
}
25+
const customAttributes = CustomAttributes.getCustomAttributes( attributes, 'blockLinkCustomAttributes' )
2426

2527
return (
2628
<Link.Content
29+
{ ...customAttributes }
2730
className="stk-block-link stk--transparent-overlay"
2831
href={ href || attributes.blockLinkUrl }
2932
target={ attributes.blockLinkNewTab ? '_blank' : '' }
3033
rel={ attributes.blockLinkRel || undefined }
3134
title={ attributes.blockLinkTitle || undefined }
32-
aria-label={ attributes.blockLinkAriaLabel || undefined }
3335
aria-hidden={ isHidden ? 'true' : undefined }
3436
tabindex={ isHidden ? '-1' : undefined }
3537
/>
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
export const addAttributes = attrObject => {
1+
export const addAttributes = ( attrObject, options = {} ) => {
2+
const {
3+
attrNameTemplate = '%s',
4+
versionAdded = '3.0.0',
5+
} = options
6+
27
attrObject.add( {
38
attributes: {
49
customAttributes: {
510
type: 'array',
611
default: [],
712
},
813
},
9-
versionAdded: '3.0.0',
14+
attrNameTemplate,
15+
versionAdded,
1016
versionDeprecated: '',
1117
} )
1218
}

‎src/block-components/custom-attributes/edit.js‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,31 @@ import { useBlockAttributesContext, useBlockSetAttributesContext } from '~stacka
1717
* WordPress dependencies
1818
*/
1919
import { __ } from '@wordpress/i18n'
20+
import { ExternalLink } from '@wordpress/components'
2021
import { INVALID_HTML_ATTRIBUTES } from '.'
2122

22-
export const Edit = () => {
23-
const customAttributes = useBlockAttributesContext( attributes => attributes.customAttributes )
23+
const CustomAttributesHelp = ( { hasLink } ) => {
24+
return <p className="components-base-control__help">
25+
{ hasLink
26+
? __( 'You can type in custom HTML attributes for this block or its link in the fields above. Examples:', i18n )
27+
: __( 'You can type in custom HTML attributes for this block in the field above. Example:', i18n ) }
28+
<br />
29+
<code>data-id=&quot;my-title&quot;</code>
30+
{ hasLink && <>
31+
<br />
32+
<code>aria-label=&quot;Open product details&quot;</code>
33+
</> }
34+
</p>
35+
}
36+
37+
export const Edit = props => {
38+
const {
39+
customAttributes,
40+
linkCustomAttributes,
41+
} = useBlockAttributesContext( attributes => ( {
42+
customAttributes: attributes.customAttributes,
43+
linkCustomAttributes: props.linkAttributeName ? attributes[ props.linkAttributeName ] : undefined,
44+
} ) )
2445
const setAttributes = useBlockSetAttributesContext()
2546

2647
return (
@@ -30,12 +51,30 @@ export const Edit = () => {
3051
id="custom-attributes"
3152
>
3253
<CustomAttributesControl
33-
label={ __( 'Custom Attributes', i18n ) }
54+
label={ props.linkAttributeName ? __( 'Block Custom Attributes', i18n ) : __( 'Custom Attributes', i18n ) }
3455
value={ customAttributes }
3556
invalidHtmlAttributes={ INVALID_HTML_ATTRIBUTES }
3657
onChange={ customAttributes => setAttributes( { customAttributes } ) }
3758
/>
59+
{ props.linkAttributeName && <CustomAttributesControl
60+
label={ __( 'Link Custom Attributes', i18n ) }
61+
value={ linkCustomAttributes }
62+
invalidHtmlAttributes={ INVALID_HTML_ATTRIBUTES }
63+
onChange={ linkCustomAttributes => setAttributes( { [ props.linkAttributeName ]: linkCustomAttributes } ) }
64+
/>
65+
}
66+
<CustomAttributesHelp hasLink={ !! props.linkAttributeName } />
67+
<ExternalLink
68+
href="https://docs.wpstackable.com/article/461-how-to-use-custom-attributes?utm_source=inspector&utm_campaign=learnmore&utm_medium=gutenberg"
69+
target="_docs"
70+
>
71+
{ __( 'Learn more about Custom Attributes', i18n ) }
72+
</ExternalLink>
3873
</PanelAdvancedSettings>
3974
</InspectorAdvancedControls>
4075
)
4176
}
77+
78+
Edit.defaultProps = {
79+
linkAttributeName: '',
80+
}

‎src/block-components/custom-attributes/index.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ CustomAttributes.addAttributes = addAttributes
2626

2727
// CustomAttributes.Style = Style
2828

29-
CustomAttributes.getCustomAttributes = attributes => {
30-
if ( ! Array.isArray( attributes.customAttributes ) || attributes.customAttributes.length === 0 ) {
29+
CustomAttributes.getCustomAttributes = ( attributes, attributeName = 'customAttributes' ) => {
30+
if ( ! Array.isArray( attributes[ attributeName ] ) || attributes[ attributeName ].length === 0 ) {
3131
return {}
3232
}
3333

34-
const customAttributes = Object.fromEntries( attributes.customAttributes )
34+
const customAttributes = Object.fromEntries( attributes[ attributeName ] )
35+
const invalidBlockAttributes = [ ...INVALID_BLOCK_ATTRIBUTES, attributeName ]
3536
Object.keys( customAttributes ).forEach( key => {
3637
// Unescape the value, since we're storing them as escaped strings.
3738
let value = unescape( customAttributes[ key ] )
@@ -44,7 +45,7 @@ CustomAttributes.getCustomAttributes = attributes => {
4445
dynamicAttributeMatch.forEach( _match => {
4546
const match = _match.substr( 1, _match.length - 2 )
4647
if (
47-
! INVALID_BLOCK_ATTRIBUTES.includes( match ) &&
48+
! invalidBlockAttributes.includes( match ) &&
4849
attributes.hasOwnProperty( match ) &&
4950
! isUndefined( attributes[ match ] )
5051
) {
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
# Custom Attributes Block Component
22

3-
Gives the ability to add your own custom attributes which will be placed on the block.
3+
Gives the ability to add custom attributes to a block or its generated link.
44

55
## Usage
66

7-
The custom attribute implementation is already handled by the Block Div Block Component
7+
The block custom attribute implementation is already handled by the Block Div Block Component.
8+
For a link, render the attributes with `CustomAttributes.getCustomAttributes( attributes, 'linkCustomAttributes' )` or its block-link equivalent.
89

910
### Adding inspector controls in `edit.js`
1011

1112
```
1213
<CustomAttributes.InspectorControls />
1314
```
1415

16+
For a block with a configurable link, pass the link attribute name to add a second field below the block attributes.
17+
18+
```
19+
<CustomAttributes.InspectorControls linkAttributeName="linkCustomAttributes" />
20+
```
21+
1522
### Adding attributes in `schema.js`
1623

1724
```
1825
CustomAttributes.addAttributes( attrObject )
1926
```
27+
28+
Link attributes are registered through `Link.addAttributes` or `BlockLink.addAttributes`.

‎src/block-components/helpers/link/attributes.js‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { addAttributes as addCustomAttributes } from '../../custom-attributes/attributes'
2+
13
export const linkAttributes = ( selector = 'a' ) => {
24
return {
35
hasLink: {
@@ -51,18 +53,8 @@ export const addLinkAttributes = ( attrObject, attrNameTemplate = '%s', selector
5153
versionDeprecated: '',
5254
} )
5355

54-
attrObject.add( {
55-
attributes: {
56-
ariaLabel: {
57-
type: 'string',
58-
source: 'attribute',
59-
selector,
60-
attribute: 'aria-label',
61-
default: '',
62-
},
63-
},
56+
addCustomAttributes( attrObject, {
6457
attrNameTemplate,
6558
versionAdded: '3.20.1',
66-
versionDeprecated: '',
6759
} )
6860
}

‎src/block-components/helpers/link/edit.js‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export const LinkControls = props => {
2929
const hasLightboxAttr = useAttributeValue( 'hasLightbox', props.attrNameTemplate )
3030
const rel = useAttributeValue( 'rel', props.attrNameTemplate )
3131
const title = useAttributeValue( 'title', props.attrNameTemplate )
32-
const ariaLabel = useAttributeValue( 'ariaLabel', props.attrNameTemplate )
3332
const hasTitleAttr = useAttributeValue( 'hasTitle', props.attrNameTemplate )
3433

3534
const showGoogleMapHint = hasLightboxAttr &&
@@ -77,14 +76,6 @@ export const LinkControls = props => {
7776
value={ rel }
7877
onChange={ updateAttributeHandler( 'rel' ) }
7978
/>
80-
<AdvancedTextControl
81-
label={ __( 'Link accessibility label', i18n ) }
82-
help={ __( 'Describe the purpose of the link for screen reader users.', i18n ) }
83-
value={ ariaLabel }
84-
onChange={ updateAttributeHandler( 'ariaLabel' ) }
85-
isDynamic={ true }
86-
isFormatType={ false }
87-
/>
8879
{ ( hasTitle || hasTitleAttr ) && (
8980
<AdvancedTextControl
9081
label={ __( 'Link Title', i18n ) }

‎src/block-components/link/index.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import { addAttributes } from './attributes'
55
import { Edit } from './edit'
6+
import { CustomAttributes } from '../custom-attributes'
67

78
/*+
89
* External dependencies
@@ -42,20 +43,21 @@ Link.Content = props => {
4243
linkProps = {},
4344
attributes,
4445
} = props
46+
const customAttributes = CustomAttributes.getCustomAttributes( attributes, 'linkCustomAttributes' )
4547

4648
if ( ! attributes.linkHasLink ) {
4749
return props.children
4850
}
4951

5052
return (
5153
<LinkComponent.Content
54+
{ ...customAttributes }
5255
{ ...linkProps }
5356
className={ props.className }
5457
href={ attributes.linkUrl || undefined }
5558
target={ attributes.linkNewTab ? '_blank' : undefined }
5659
rel={ attributes.linkRel || undefined }
5760
title={ attributes.linkTitle || undefined }
58-
aria-label={ attributes.linkAriaLabel || undefined }
5961
>
6062
{ props.children }
6163
</LinkComponent.Content>

‎src/block/call-to-action/edit.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ const InspectorControls = memo( () => {
137137
<Transform.InspectorControls />
138138
<Separator.InspectorControls />
139139
<EffectsAnimations.InspectorControls />
140-
<CustomAttributes.InspectorControls />
140+
<CustomAttributes.InspectorControls linkAttributeName="blockLinkCustomAttributes" />
141141
<CustomCSS.InspectorControls mainBlockClass="stk-block-call-to-action" />
142142
<Responsive.InspectorControls />
143143
<ConditionalDisplay.InspectorControls />

‎src/block/card/edit.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ const InspectorControls = memo( props => {
191191
<Advanced.InspectorControls />
192192
<Transform.InspectorControls />
193193
<EffectsAnimations.InspectorControls />
194-
<CustomAttributes.InspectorControls />
194+
<CustomAttributes.InspectorControls linkAttributeName="blockLinkCustomAttributes" />
195195
<CustomCSS.InspectorControls mainBlockClass="stk-block-card" />
196196
<Responsive.InspectorControls />
197197
<ConditionalDisplay.InspectorControls />

0 commit comments

Comments
 (0)