Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Conversation

@tannerdolby
Copy link
Contributor

Fixes #586

}

// if the original target is not an `example-choice` element
if (!target.classList.contains('example-choice')) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tannerdolby What I find curious is how we even get into this conditional if the target does not contains a class with the name example-choice

Copy link
Contributor Author

@tannerdolby tannerdolby Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@schalkneethling I found that a bit curious as well. It works, but is confusing at first glance. Its essentially saying if a user clicks inside the <code> element in example choices and not within the parent <div class="example-choice"> container then find the nearest parent example choice div with findParentChoiceElem.

So if we click directly on the edge/border of example choices, the event.target will be the <div class="example-choice"> which does have the class 'example-choice' so the if statement is not entered and the target is passed to mce.onChoose(target). If we click inside the example choices, the event.target will be the <code class="language-*"> elements and there won't have the .example-choice class so we enter the conditional block and find the nearest parent element with mce.findParentChoiceElem(target). Below is a short demo.

target-demo.mov

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tannerdolby What if we do this. We add the following function:

function handleChoiceEvent() {
    if (this.classList.contains('copy')) {
        mceAnalytics.trackEvent({
            category: 'Interactive Example - CSS',
            action: 'Copy to clipboard clicked',
            label: 'Interaction Events',
        });
    }

    // and pass it on to `onChoose`
    module.exports.onChoose(this);
}

and then we replace this block of code with:

const exmapleChoices = exampleChoiceList.querySelectorAll(
        '.example-choice'
    );
    Array.from(exmapleChoices).forEach((choice) => {
        choice.addEventListener('click', handleChoiceEvent);
    });

Copy link
Contributor Author

@tannerdolby tannerdolby Jul 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this looks good!

@schalkneethling schalkneethling self-requested a review July 14, 2021 10:43
@tannerdolby
Copy link
Contributor Author

Should be good to go now πŸŽ‰ @schalkneethling

Copy link

@schalkneethling schalkneethling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @tannerdolby πŸŽ‰

@schalkneethling schalkneethling merged commit b3285fb into mdn:master Jul 14, 2021
@tannerdolby tannerdolby deleted the 586-handle-click-between-choices branch July 14, 2021 20:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clicking in between example choices confuses findParentChoiceElem in mce-utils.js

2 participants