Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Don't create a close button automatically #11

Merged
merged 8 commits into from
Apr 9, 2018
Merged

Conversation

shawnbot
Copy link
Contributor

@shawnbot shawnbot commented Apr 4, 2018

Removes the automatically added close button in favor or allowing implementors to supply their own. Originally it did this:

Fixes #10 by checking for whether a close button ([data-close-dialog]) exists in the DOM before adding one automatically. There's a simple test for it, which passes! 🎉

But after some discussion, we agreed that nixing the close button altogether was the best move.

I've also introduced CLOSE_ATTR, CLOSE_SELECTOR, and INPUT_SELECTOR constants to keep things dry, and exposed them as static members of the (now default-exported) custom element class, which means that you can do this in other modules, e.g. in React:

import DetailsDialog from 'details-dialog-element'
import React from 'react'

const closeProps = {
  [DetailsDialog.CLOSE_ATTR]: true
}

export default ({children, ...rest}) => (
  <details-dialog {...rest}>
    {children}
    <button {...closeProps}>Close me!</button>
  </details-dialog>
)

@shawnbot shawnbot requested review from dgraham and muan April 4, 2018 21:52
@dgraham
Copy link
Contributor

dgraham commented Apr 4, 2018

What do you think about removing the default close button from the element's responsibilities and leave it to the host application to provide one? That would leave the custom element responsible for its open states, form reset on close, and tab behavior.

Because the element is responsible for open and close it makes sense that it includes an Escape key handler and click handler on the modal backdrop to close itself. Including a visual close button feels more like the responsibility of the app.

The data-close-dialog attribute is a lightweight way to participate in the element's state that we could still support.

Including the close button feels a little prescriptive when multiple presentations are possible. For example, here's this custom element with close button icon in the corner.

screen shot 2018-04-04 at 16 42 57

And here's another possible handling of a text labeled close button positioned at the bottom.

screen shot 2018-04-04 at 16 31 21

Another sign of tension in these responsibilities is the inline octicon svg from Primer. Removing the default button would let the host app render icons from its preferred style library. We would use the element with Primer like this in the Rails app.

<details>
  <details-dialog>
    <button type="button" class="dd-close-button" aria-label="Close" data-close-dialog>
      <%= octicon("x") %>
    </button>
  </details-dialog>
</details>

The example React usage above could provide the close button from a library function or inline JSX.

@shawnbot
Copy link
Contributor Author

shawnbot commented Apr 4, 2018

I'm down with that, @dgraham. Another way we could handle it is to provide the button for screen readers only, so that it becomes purely an accessibility feature. In that case, maybe you would be able to opt out of it explicitly, e.g. with close-button="false" or something?

@muan
Copy link
Contributor

muan commented Apr 5, 2018

🙌🏻 All very good and valid points. This was first added to replicate what facebox has.

Including a visual close button feels more like the responsibility of the app.

Agree. I'm for removing the close button altogether and let the user decide on what to do. This way we avoid having to assume style and pulling in octicon.

I think we can also get away from not providing a screen reader only button assuming Escape key is a common pattern for escaping dialogs.

return CLOSE_ATTR
}
static get CLOSE_SELECTOR() {
return CLOSE_SELECTOR
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a great idea!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you all have a preference for this over exporting it at the module level?

Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer this.

@shawnbot
Copy link
Contributor Author

shawnbot commented Apr 5, 2018

Uhh also, why does this work when the selector is missing the ]?

const close = dialog.querySelector('[data-close-dialog')
assert(!details.open)
dialog.toggle(true)
assert(details.open)
close.click()
assert(!details.open)

@shawnbot shawnbot changed the title Make the automatically added close button optional Don't create a close button automatically Apr 5, 2018
@shawnbot
Copy link
Contributor Author

shawnbot commented Apr 5, 2018

Okay, I've removed the automatically added close button and the corresponding CSS, and marked the failing tests as pending. I also updated the tests to use the constants, but I'm honestly not sure whether that's good practice or not. 😬 Ready for review?

Copy link
Contributor

@muan muan left a comment

Choose a reason for hiding this comment

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

Looks good! Merge master for test fix in #12?

return CLOSE_ATTR
}
static get CLOSE_SELECTOR() {
return CLOSE_SELECTOR
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer this.

@shawnbot shawnbot merged commit 3f19c99 into master Apr 9, 2018
@shawnbot shawnbot deleted the optional-close-button branch April 9, 2018 18:48
@muan muan mentioned this pull request Apr 10, 2018
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.

Opt out (or in?) to close button?
3 participants