Skip to content

Commit 1d32e4d

Browse files
Use HTMLDetailsElement detection from GOV.UK Frontend
1 parent 98eecb3 commit 1d32e4d

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

packages/components/details/details.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ const { toggleAttribute } = require('../../common')
66
*/
77

88
module.exports = ({ scope = document } = {}) => {
9-
// Does the browser support details component
10-
const nativeSupport =
11-
typeof document.createElement('details').open === 'boolean'
12-
if (nativeSupport) {
13-
return
14-
}
15-
169
// Nodelist of all details elements
1710
const allDetails = scope.querySelectorAll('details')
1811

@@ -77,10 +70,13 @@ module.exports = ({ scope = document } = {}) => {
7770
})
7871
}
7972

80-
// Initialise details for any new details element
81-
if (allDetails.length) {
82-
allDetails.forEach((element, index) => {
83-
if (!element.hasAttribute('nhsuk-polyfilled')) initDetails(element, index)
84-
})
85-
}
73+
allDetails.forEach((element, index) => {
74+
// If there is native details support, we want to avoid running code to polyfill native behaviour.
75+
const hasNativeDetails =
76+
'HTMLDetailsElement' in window && element instanceof HTMLDetailsElement
77+
78+
if (!hasNativeDetails && !element.hasAttribute('nhsuk-polyfilled')) {
79+
initDetails(element, index)
80+
}
81+
})
8682
}

0 commit comments

Comments
 (0)