-
Notifications
You must be signed in to change notification settings - Fork 232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update components to use Component
#4205
Conversation
✅ You can preview this change here:
To edit notification comments on pull requests, go to your Netlify site configuration. |
e4aba67
to
b5131f8
Compare
b5131f8
to
fb6b12f
Compare
24202c8
to
9484b8f
Compare
3b110db
to
9484b8f
Compare
Apologies for the push on this branch, that was meant for a separate PR for discussion 😬 |
* | ||
* @throws {Error} when component not supported | ||
*/ | ||
checkSupport() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue GOV.UK Frontend's Component
will call the static checkSupport
method, which is unchanged, and not run this code. It's fine to keep these checks in the constructor as they need to access this.$root
😊
src/javascripts/application.mjs
Outdated
@@ -48,7 +48,7 @@ createAll(Copy) | |||
new OptionsTable() | |||
|
|||
// Initialise mobile navigation | |||
new Navigation(document) | |||
new Navigation(document.body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion Given the Navigation
component now has a moduleName
, how about adding a data-module="app-navigation"
on the <body>
and using createAll
.
This would handle any errors thrown where GOV.UK Frontend is not supported, as those would prevent further code in this script to execute at the moment.
// Exit if we're on the cookies page to avoid circular journeys | ||
this.onCookiesPage() | ||
) { | ||
return this | ||
} | ||
|
||
this.$cookieBanner = $module | ||
this.$cookieBanner = this.$root |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion We may as well replace this.$cookieBanner
by this.$root
in the whole component.
if ( | ||
!($module instanceof HTMLElement) || | ||
!document.body.classList.contains('govuk-frontend-supported') || | ||
// Exit if we're on the cookies page to avoid circular journeys | ||
this.onCookiesPage() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question If we make onCookiesPage()
static or even a function in the module, could this be part of checkSupport()
?
|
||
this.$page = $module | ||
this.$page = this.$root |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion Similarly to the cookie-banner, I think we can use this.$root
instead of this.$page
across the whole component rather than assign to another instance variable.
984f448
to
cf49fcf
Compare
- Rename `this.$module` to `$this.$root` - Remove initialisation checks that occur in `Component`
- Rename `this.$module` to `$this.$root` - Remove initialisation checks that occur in `Component` - `onCookiesPage` now a static function - Use `isSupported` function to check component support
- Rename `this.$module` to `$this.$root` - Remove initialisation checks that occur in `Component`
- Rename `this.$module` to `$this.$root` - Remove initialisation checks that occur in `Component` - Use `isSupported` function to check component support
- Rename `this.$module` to `$this.$root` - Remove initialisation checks that occur in `Component` - Use `isSupported` function to check component support - Update `EmbedCard` initialisation code in `application.mjs` to prevent error on multiple initialisation and catch the error thrown if user has not accepted campaign cookies
- Rename `this.$module` to `$this.$root` - Remove initialisation checks that occur in `Component`
- Rename `this.$module` to `$this.$root` in `Navigation` - Remove initialisation checks that occur in `Component` in `Navigation` - Use `createAll` in `application.mjs` to initialise component (so that errors thrown are caught) - Add `data-module="app-navigation"` to `body` of page instead of initialising `Navigation` on `document` (which is now no longer supported due to the new initialisation method that sets attributes) so `createAll(Navigation)` can be used
- Rename `this.$module` to `$this.$root` - Remove initialisation checks that occur in `Component` - Use `isSupported` function to check component support
- Rename `this.$module` to `$this.$root` - Remove initialisation checks that occur in `Component`
- Rename `this.$module` to `$this.$root` - Remove initialisation checks that occur in `Component`
As govuk-frontend provides no types, TypeScript will type its exports as `any`, but be unable to acknowledge fields inherited from parent classes leading to errors when trying to assign or use them. TypeScript's shorthand ambient modules seem to also make inherited fields typed as `any`. https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#shorthand-ambient-module-declarations This will override any other definition of the `govuk-frontend` module, either in another `.d.ts` file or in `@types/govuk-frontend`.
cf49fcf
to
04a7ba2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Thanks for splitting it up component by component as well 🙌🏻 ⛵
What
Make components in
govuk-design-system
extendComponent
. This means we can:this.$module
to$this.$root
in a given componentComponent
in a given componentisSupported
function to check component support in a given componentIn addition, changes were needed to
EmbedCard
andNavigation
in light of changes to how components are now initialised.In this PR, we also add configuration for Typescript to look up
.ts
files, which allows us to be able to add a shorthand ambient module definition forgovuk-frontend
. This means Typescript/JSDoc will not throw any errors when callingthis.$root
in a class that extendsComponent
.Why
Component
is now available as ofv5.7.0
ofgovuk-frontend
. We also have made changes to initialisation to prevent multi-initialisation occurring which means usingbody
as a root module is now no longer supported.