Skip to content

Commit

Permalink
feat: added download attribute for buttons link
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayank-Tripathi32 committed Jan 7, 2025
1 parent f62da11 commit 4caeb5f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Prompt visitors to take action with a button-style link. ([Source](https://githu
- **Category:** design
- **Parent:** core/buttons
- **Supports:** anchor, color (background, gradients, text), interactivity (clientNavigation), shadow (), spacing (padding), splitting, typography (fontSize, lineHeight), ~~alignWide~~, ~~align~~, ~~reusable~~
- **Attributes:** backgroundColor, gradient, linkTarget, placeholder, rel, tagName, text, textAlign, textColor, title, type, url, width
- **Attributes:** backgroundColor, download, gradient, linkTarget, placeholder, rel, tagName, text, textAlign, textColor, title, type, url, width

## Buttons

Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
"attribute": "rel",
"role": "content"
},
"download": {
"type": "boolean",
"source": "attribute",
"selector": "a",
"attribute": "download",
"role": "content"
},
"placeholder": {
"type": "string"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ const LINK_SETTINGS = [
id: 'nofollow',
title: __( 'Mark as nofollow' ),
},
{
id: 'download',
title: __( 'Download file' ),
},
];

function useEnter( props ) {
Expand Down Expand Up @@ -380,13 +384,15 @@ function ButtonEdit( props ) {
url: newURL,
opensInNewTab: newOpensInNewTab,
nofollow: newNofollow,
download: newDownload,
} ) =>
setAttributes(
getUpdatedLinkAttributes( {
rel,
url: newURL,
opensInNewTab: newOpensInNewTab,
nofollow: newNofollow,
download: newDownload,
} )
)
}
Expand Down
26 changes: 26 additions & 0 deletions packages/block-library/src/button/get-updated-link-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import { prependHTTP } from '@wordpress/url';
* @param {string} attributes.url The current link url.
* @param {boolean} attributes.opensInNewTab Whether the link should open in a new window.
* @param {boolean} attributes.nofollow Whether the link should be marked as nofollow.
* @param {boolean} attributes.download Whether the link should allow download.
*/
export function getUpdatedLinkAttributes( {
rel = '',
url = '',
opensInNewTab,
nofollow,
download = false,
} ) {
let newLinkTarget;
// Since `rel` is editable attribute, we need to check for existing values and proceed accordingly.
Expand All @@ -46,9 +48,33 @@ export function getUpdatedLinkAttributes( {
updatedRel = updatedRel?.replace( relRegex, '' ).trim();
}

const allowDownload = url && isSameOrigin( url ) ? download : undefined;

return {
url: prependHTTP( url ),
linkTarget: newLinkTarget,
rel: updatedRel || undefined,
download: allowDownload,
};
}

/**
* Checks if the URL is same origin.
* Allow relative URLs.
*
* @param {string} urlString The URL to check.
* @return {boolean} Whether the URL is same origin.
*/
function isSameOrigin( urlString ) {
// Allow relative URLs
if ( urlString.startsWith( '/' ) ) {
return true;
}

try {
const url = new URL( urlString, window.location.origin );
return url.origin === window.location.origin;
} catch {
return false;
}
}
2 changes: 2 additions & 0 deletions packages/block-library/src/button/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default function save( { attributes, className } ) {
title,
url,
width,
download,
} = attributes;

const TagName = tagName || 'a';
Expand Down Expand Up @@ -83,6 +84,7 @@ export default function save( { attributes, className } ) {
value={ text }
target={ isButtonTag ? null : linkTarget }
rel={ isButtonTag ? null : rel }
download={ isButtonTag ? null : download }
/>
</div>
);
Expand Down

0 comments on commit 4caeb5f

Please sign in to comment.