Skip to content
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

Use the connectedCallback function to set button styles instead of the constructor #55

Merged
merged 5 commits into from
Jan 5, 2022

Conversation

koddsson
Copy link
Contributor

@koddsson koddsson commented Jan 4, 2022

Building a header button in memory with a custom heading ("level") causes a unexpected behaviour. Consider the following example:

const headerButton = document.createElement('md-header') // The constructor is executed at this point.
headerButton.setAttribute('level', '1')                  // Has no effect since the style has already been set.
headerButton.textContent = 'h1'
document.body.appendChild(headerButton)

While we expect to have a header button that outputs a single hash (#), we have a button that outputs three (###). The constructor sets the styles based on the level attribute when the component is initialised. Changing the constructor to a connectedCallback function ensures that the button styles aren't set until the button is connected to the DOM.

Since constructor can be error-prone for custom elements and keep all the components similar, I've also changed all the constructors to connectedCallback functions.

Finally, even though we don't expect header buttons to change levels dynamically during an application lifetime, we should ensure that it works as expected. So I've added an attributeChangedCallback to the header button element. The callback changes the level style based on the level attribute.

Fixes #40 and possibly #45

The test shows that changes to the `level` attribute of a markdown
header button aren't reflected in the output when pressing the button.
Building a header button in memory with a custom heading ("level")
causes a unexpected behaviour. Consider the following example:

```js
const headerButton = document.createElement('md-header') // The constructor is executed at this point.
headerButton.setAttribute('level', '1')                  // Has no effect since the style has already been set.
headerButton.textContent = 'h1'
document.body.appendChild(headerButton)
```

While we expect to have a header button that outputs a single hash
(`#`), we have a button that outputs three (`###`). The constructor sets
the styles based on the `level` attribute when the component is
initialised.

Changing the constructor to a `connectedCallback` function
ensures that the button styles aren't set until the button is connected
to the DOM.
`constructor` will call the `super` function automatically. We don't
have to allocate code to do that as well.
Since `constructor` can be error-prone for custom elements and keep all
the components similar, I've also changed all the constructors to
`connectedCallback` functions.
Even though we don't expect header buttons to change levels dynamically
during an application lifetime, we should ensure that it works as
expected.

So I've added an `attributeChangedCallback` to the header
button element. The callback changes the level style based on the
`level` attribute.
@koddsson koddsson marked this pull request as ready for review January 4, 2022 17:19
@koddsson koddsson requested a review from a team as a code owner January 4, 2022 17:19
@koddsson koddsson merged commit a068e8f into main Jan 5, 2022
@koddsson koddsson deleted the use-connectedcallback branch January 5, 2022 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

this.getAttribute() returns undefined when used with async components
2 participants