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

🧹 Add HedySelect custom element #5540

Merged
merged 49 commits into from
Jul 30, 2024
Merged

🧹 Add HedySelect custom element #5540

merged 49 commits into from
Jul 30, 2024

Conversation

jpelay
Copy link
Member

@jpelay jpelay commented May 16, 2024

One of the advantages of frameworks is that they allow you to define custom HTML elements and couple them with JavaScript behavior. Since a few years ago, vanilla JavaScript now supports the notion of custom elements as part of the wider technologies used to support web components. The basic notion here is that we can define a template that will hold the HTML code for our element, and then clone that template and attach it to our DOM, at the same time executing a function that will be called once the component is connected.

There's another way to show the component, by using the shadow DOM. This will further encapsulate the element, by hiding it's inner components from the general application, however this has the disadvantage of not being able to use tailwind in this component, since it can't access the CSS stylesheets from the application. Due to this limitation I decided to just attach the element to the old fashioned DOM.

The basic structure of the components is the following:

  • A template defined in custom-element-templates.html
  • A class defined in custom-elements.ts
  • The class is registered in the customElements registry.

After doing this we can just use the component within our HTML like we'd do with any other component. With the example of the selects, we used to have a prepareDropdowns function that was called on page load. Now, that function is called for every select once they're connected using the connectedCallback property. The usage of the select in the HTML is like this:

<hedy-select id="select_id">
    <select multiple>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
</hedy-select>

That inner select will be used to generate the appropriate divs that will make the options, and will eventually be hidden from the document.

To get the selected items, we just need to query our desired hedy-select and use the selected property; this will return a string array with the selected options. Notice that if the select is single, you can just select the first element of the array.

(document.querySelector('#levels_dropdown') as HedySelect).selected

Going forward we can use this idea to replace other parts of our code base that can be made into single, reusable components. Do you guys have any ideas about this?

How to test

Everything should work normally

@jpelay jpelay self-assigned this May 21, 2024
@jpelay
Copy link
Member Author

jpelay commented Jul 17, 2024

@jpelay one point we also need to implement is when the select element is required; e.g,. in the level select in the customize-adventure page. Would that be possible?

I actually handle it on case by case basis, on that particular case in the function update_db_adventure I check if there are no selected levels and notify the user if that is the case.

@jpelay
Copy link
Member Author

jpelay commented Jul 18, 2024

I addressed every comment!

@Felienne Felienne dismissed hasan-sh’s stale review July 30, 2024 12:18

All addressed now!

Copy link
Contributor

mergify bot commented Jul 30, 2024

Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork).

Copy link
Contributor

mergify bot commented Jul 30, 2024

Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 69cb01f into main Jul 30, 2024
12 checks passed
Copy link
Contributor

mergify bot commented Jul 30, 2024

Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot deleted the generic-dropdown branch July 30, 2024 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants