Skip to content

Commit 0a2c3e0

Browse files
Bastiaan Snelderjsamr
Bastiaan Snelder
authored andcommitted
feat: support custom data- attributes in HandleLinkPressFeature
1 parent c7d1e48 commit 0a2c3e0

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

apps/website/typedoc-sidebars.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = [
1818
"api/interfaces/domrect",
1919
"api/interfaces/domrectsize",
2020
"api/interfaces/domutils",
21+
"api/interfaces/dataattributes",
2122
"api/interfaces/elementcssboxdimensions",
2223
"api/interfaces/featurebuilderconfig",
2324
"api/interfaces/featureclass",

packages/webshell/etc/webshell.api.md

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export interface CSSBoxDimensionsComputedStyle {
5959
paddingTop: number;
6060
}
6161

62+
// @public
63+
export interface DataAttributes {
64+
// (undocumented)
65+
[key: string]: string;
66+
}
67+
6268
// @public
6369
export type DOMCollectionRequest = DOMElementQueryRequest | DOMElementClassNameRequest | DOMElementTagNameRequest | string;
6470

@@ -293,6 +299,7 @@ export interface LinkPressOptions {
293299
export interface LinkPressTarget {
294300
classAttribute: string | null;
295301
clickedAnchorBoundingRect: DOMRect;
302+
dataAttributes: DataAttributes;
296303
downloadAttribute: string | null;
297304
hrefAttribute: string;
298305
hreflangAttribute: string | null;

packages/webshell/src/features/HandleLinkPressFeature.ts

+16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ export interface LinkPressOptions {
2525
ignoreHashChange?: boolean;
2626
}
2727

28+
/**
29+
* An object containing the custom data attributes of the anchor which has been clicked.
30+
*
31+
* @public
32+
*/
33+
export interface DataAttributes {
34+
[key: string]: string;
35+
}
36+
2837
/**
2938
* The target of a link press event.
3039
*
@@ -79,6 +88,13 @@ export interface LinkPressTarget {
7988
* The `name` attribute, if present.
8089
*/
8190
nameAttribute: string | null;
91+
/**
92+
* The `data-` attributes, if present.
93+
*
94+
* @remarks
95+
* Dashes are converted to camelCase. See {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset | HTMLElement.dataset}.
96+
*/
97+
dataAttributes: DataAttributes;
8298
/**
8399
* The bounding rectangle of the anchor which has been clicked.
84100
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect | Element.getBoundingClientRect()}

packages/webshell/src/features/HandleLinkPressFeature.webjs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function HandleLinkPressFeature(context) {
5656
idAttribute: anchor.getAttribute('id'),
5757
classAttribute: anchor.getAttribute('class'),
5858
nameAttribute: anchor.getAttribute('name'),
59+
dataAttributes: anchor.dataset,
5960
clickedAnchorBoundingRect: clickedAnchorBoundingRect,
6061
page: {
6162
href: window.location.href,

packages/webshell/src/features/__tests__/HandleLinkPressFeature.test.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('Webshell with HandleLinkPressFeature', () => {
1818
onDOMLinkPress={onDOMLinkPress}
1919
source={{
2020
html:
21-
'<a id="anchor0" class="link" type="text/html" rel="alternate" download="" target="_blank" hreflang="en" referrerpolicy="no-referrer" href="https://foo.org">bar</a>'
21+
'<a id="anchor0" class="link" type="text/html" rel="alternate" download="" target="_blank" hreflang="en" referrerpolicy="no-referrer" href="https://foo.org" data-webshell="custom">bar</a>'
2222
}}
2323
/>
2424
)
@@ -37,6 +37,9 @@ describe('Webshell with HandleLinkPressFeature', () => {
3737
idAttribute: 'anchor0',
3838
classAttribute: 'link',
3939
nameAttribute: null,
40+
dataAttributes: {
41+
webshell: 'custom'
42+
},
4043
clickedAnchorBoundingRect: {
4144
top: expect.any(Number),
4245
left: expect.any(Number),

0 commit comments

Comments
 (0)