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

Pass 'close' as closure action #230

Merged

Conversation

caseywatts
Copy link
Contributor

This adds in the ability to close the flash message in your own custom components. I tweaked the example in the readme to include a close button in the template there. I've also tried this in my own app :)

If we like this idea, we should probably make a separate example that has close on click set to false (since currently this example it wouldn't quite make sense why we want the close button when you can click on the whole thing to close it).

@caseywatts
Copy link
Contributor Author

I'm not 100% sure I want to yield it as another ordered parameter, but I don't quite wanna make a separate actions hash for it either lol
For this concern, I'm mostly worried about future edits to this addon - it works great as-is, no problem ✨

@caseywatts
Copy link
Contributor Author

I tried writing some tests for this feature, but they all suck so far lol:
#231

@caseywatts
Copy link
Contributor Author

This branch has an integration test now - this PR should be good to go! :)

@caseywatts caseywatts force-pushed the pass-close-as-closure-action branch 2 times, most recently from 1c510d1 to b2d6660 Compare May 30, 2017 15:32
@caseywatts caseywatts force-pushed the pass-close-as-closure-action branch from b2d6660 to 61a62dd Compare May 30, 2017 15:40
@caseywatts
Copy link
Contributor Author

@sbatson5 could you look over this when you get a chance? :)

@sbatson5
Copy link
Collaborator

@caseywatts sorry for the delay. The test looks good. Just want to pull it into a dummy app real quick and test it. Should be able to get to that tonight

@caseywatts
Copy link
Contributor Author

sweet! thanks @sbatson5 :)

Copy link
Collaborator

@sbatson5 sbatson5 left a comment

Choose a reason for hiding this comment

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

I'm wondering if we can expand this to be a bit more flexible. Rather than adding a closure action to the component, we could add a flashAction property to the flash object itself. This keeps us from yielding another property and would make it easier to control the destroyOnClick logic. We could also have the label of the action button as another property. So we could create messages like:

get(this, 'flashMessages').success('Success!!', {
  flashAction: {
    label: 'Close'
  }
});

Then we could add a default action property that calls flash.destroyMessage(). Or the user could overwrite it with their own action if they wanted to do something other than closing (i.e. they wanted to rollback attributes on a model or something similar).

Although this adds some extra conditions to the template:

{{#flash-message flash=flash}}
    {{flash.message}}
    {{#if flash.flashAction}}
      <a href="#" {{action (action flash.flashAction.action)}}>
        {{flash.flashAction.label}}
      </a>
    {{/if}}
  {{/flash-message}}

So if the user sets a flashAction, we render it with their label and default flashAction.action to destroyMessage, otherwise we use an action they pass in.

This may be overcomplicating the API, however. Let me know your thoughts!
cc @poteto

@@ -1,5 +1,5 @@
{{#if hasBlock}}
{{yield this flash}}
{{yield this flash (action 'close')}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

For consistency, we try to use double quotes on templates. Can you wrap close in double quotes?

this.set('flash', FlashMessage.create({
message: 'flash message content',
sticky: true,
destroyOnClick: false,
Copy link
Collaborator

Choose a reason for hiding this comment

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

🤔 I'm wondering if there is a way for us to default this to false when yielding/using the close action. It could be confusing if both are enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

uh oh right, destroyOnClick=true is the default :/
I was surprised that was true when I first read the docs for this. And I'm surprised again just now haha (I wonder if other users are surprised by this default too?)

Hmm - we could at least document it in the README.md next to an example that uses close?

@caseywatts
Copy link
Contributor Author

I imagine this is a pretty common use case and I'd love to make it super straightforward, too.

--

This is a pretty common fork-in-the-road when designing Ember components. I'm not 100% that my suggestion is the better of the two paths, but I can provide more arguments for it at least :)

contextual-component vs pass-in-more-arguments

@sbatson5 we're also going to want to put some classes on this - that'll lead us to three arguments. Next (and this is probably going too far lol): some people may want this to be a styled button. If we wanted all of these, the API for this becomes something like this:

get(this, 'flashMessages').success('Success!!', {
  flashAction: {
    action: this.get('close'),
    label: 'Close',
    classNames: 'xyz',
    tagName: 'button'
  }
});

I think a contextual component sort of thing gets around this sort of argument-passing-bloat pretty nicely :)

Also - the last argument close is optional to write, so I don't believe it'll break any existing code or examples. It should be a ~backwards-compatible change.

@sbatson5
Copy link
Collaborator

@caseywatts ok, you convinced me :). Would you mind just updating the README and just mention to disable destroyOnClick if you want to use the close action? Once that's in, we're good to merge 👍
(sorry for the delay)


actions: {
close() {
this._destroyFlashMessage();
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you think of adding a warning via Ember.Logger.warn if flash.destroyOnClick is true? Something like:

close() {
  if (get(this, 'flash.destroyOnClick')) {
    Logger.warn('This flash message has `destroyOnClick` enabled -- be sure to disable it when using the close action')
  }
  this._destroyFlashMessage();
}

Choose a reason for hiding this comment

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

@sbatson5 keep in mind that warn is stripped from Production builds: https://emberjs.com/api/#method_warn

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like the idea of this!
If it's stripped when this package is packaged (production build) then this one would be stripped out, hrm :/

How do other addons add warnings for users of the addon?

@caseywatts
Copy link
Contributor Author

updated the readme! 🎉

Copy link
Collaborator

@sbatson5 sbatson5 left a comment

Choose a reason for hiding this comment

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

Just 1 small typo. Otherwise, looks good.

README.md Outdated
@@ -273,6 +272,20 @@ It also accepts your own template:
{{/each}}
```

### Custom `close` action
The `close` action is always passed to the component whether it is used or not. It can be used to implement your own close button, such an an `x` in the top-right corner.
Copy link
Collaborator

Choose a reason for hiding this comment

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

such *as* an x.

@caseywatts
Copy link
Contributor Author

typo corrected ✨

@sbatson5 sbatson5 merged commit 29a6cf6 into adopted-ember-addons:master Jun 29, 2017
@caseywatts caseywatts deleted the pass-close-as-closure-action branch June 29, 2017 22:15
@caseywatts
Copy link
Contributor Author

and it's in! 🎉

now that master displays this option, we'll probably want to do a release soon so it matches up? cc @sbatson5 @Dhaulagiri

@sbatson5
Copy link
Collaborator

sbatson5 commented Jul 2, 2017

Unfortunately, I don't have npm access to this package, so we will need @Dhaulagiri to publish and update for us.

@Dhaulagiri
Copy link
Collaborator

published in 1.4.3 🎉

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.

4 participants