-
Notifications
You must be signed in to change notification settings - Fork 0
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 Popover
component
#51
Draft
stormwarning
wants to merge
13
commits into
main
Choose a base branch
from
add-popover-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c1bb0d9
Update some settings
stormwarning fd9b3c7
Add normalize-keycode util
stormwarning 52dfdae
Add Popover components
stormwarning a5b97e2
Add Popover demo
stormwarning aa74949
Upgrade ember-cli-fastboot
stormwarning 35e6774
Fix addon path alias
stormwarning 0f6913c
Pass togglePopover arg to menu trigger
stormwarning 7a15ad1
Use ember-popperjs for positioning
stormwarning b4611c1
Name ref modifiers more clearly
stormwarning 0dfa1b7
Close popover when clicking outside
stormwarning 5376ea0
Update Tailwind
stormwarning 6923757
Add internal and external focusable demo elements
stormwarning 3565961
Add event handlers
stormwarning File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<PopperJS @placement="bottom-end" as |triggerRef panelRef|> | ||
{{yield | ||
(hash | ||
isOpen=this.isOpen | ||
openPopover=this.openPopover | ||
closePopover=this.closePopover | ||
Trigger=(component | ||
"popover/trigger" | ||
triggerRef=triggerRef | ||
triggerGuid=this.triggerGuid | ||
panelGuid=this.panelGuid | ||
isOpen=this.isOpen | ||
openPopover=this.openPopover | ||
closePopover=this.closePopover | ||
togglePopover=this.togglePopover | ||
) | ||
Panel=(component | ||
"popover/panel" | ||
panelRef=panelRef | ||
triggerGuid=this.triggerGuid | ||
panelGuid=this.panelGuid | ||
panelElement=this.panelElement | ||
isOpen=this.isOpen | ||
closePopover=this.closePopover | ||
) | ||
) | ||
}} | ||
</PopperJS> | ||
{{on-window "click" this.handleClickOutside}} | ||
{{on-window "focus" this.handleFocusOut}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { action } from '@ember/object'; | ||
import { guidFor } from '@ember/object/internals'; | ||
import Component from '@glimmer/component'; | ||
import { tracked } from '@glimmer/tracking'; | ||
|
||
interface PopoverArgs { | ||
className?: string; | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @export | ||
* @class Popover | ||
* @extends {Component<PopoverArgs>} | ||
* @see link to on-helper docs | ||
* @see link to ember-popperjs docs | ||
*/ | ||
export default class Popover extends Component<PopoverArgs> { | ||
guid = `${guidFor(this)}-popover`; | ||
@tracked isOpen = false; | ||
|
||
@action | ||
openPopover() { | ||
this.isOpen = true; | ||
} | ||
|
||
@action | ||
closePopover(focusableElement?: HTMLElement) { | ||
this.isOpen = false; | ||
|
||
const elementToFocus = (() => { | ||
if (!focusableElement) return this.triggerElement; | ||
if (focusableElement instanceof HTMLElement) return focusableElement; | ||
|
||
return this.triggerElement; | ||
})(); | ||
|
||
elementToFocus?.focus(); | ||
} | ||
|
||
@action | ||
togglePopover() { | ||
if (this.isOpen) { | ||
this.closePopover(); | ||
} else { | ||
this.openPopover(); | ||
} | ||
} | ||
|
||
@action | ||
handleClickOutside(event: Event) { | ||
const target = event.target as HTMLElement; | ||
|
||
if (!this.isOpen) return; | ||
if (this.triggerElement?.contains(target)) return; | ||
if (this.panelElement?.contains(target)) return; | ||
|
||
this.isOpen = false; | ||
event.preventDefault(); | ||
this.triggerElement?.focus(); | ||
} | ||
|
||
@action | ||
handleFocusOut(event: FocusEvent) { | ||
console.log('focus event', event); | ||
|
||
if (!this.isOpen) return; | ||
if (this.triggerElement?.contains(document.activeElement)) return; | ||
if (this.panelElement?.contains(document.activeElement)) return; | ||
|
||
this.closePopover(); | ||
} | ||
|
||
get triggerGuid() { | ||
return `${this.guid}-trigger`; | ||
} | ||
|
||
get panelGuid() { | ||
return `${this.guid}-panel`; | ||
} | ||
|
||
get triggerElement() { | ||
// eslint-disable-next-line | ||
// @ts-ignore | ||
return typeof FastBoot === 'undefined' | ||
? document.getElementById(this.triggerGuid) | ||
: null; | ||
} | ||
|
||
get panelElement() { | ||
return typeof FastBoot === 'undefined' | ||
? document.getElementById(this.panelGuid) | ||
: null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{{#if @isOpen}} | ||
<div | ||
id={{@panelGuid}} | ||
aria-labelledby={{@buttonGuid}} | ||
tabindex="-1" | ||
...attributes | ||
{{on "keyup" this.handleKeyUp}} | ||
{{@panelRef}} | ||
> | ||
{{yield}} | ||
</div> | ||
{{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { action } from '@ember/object'; | ||
import Component from '@glimmer/component'; | ||
|
||
import { normalizeKey } from '@showbie/backpack-ember/utils/normalize-keycode'; | ||
|
||
interface PanelArgs { | ||
isOpen: boolean; | ||
panelElement: HTMLElement; | ||
closePopover: () => void; | ||
} | ||
|
||
export default class Panel extends Component<PanelArgs> { | ||
@action | ||
handleKeyUp(event: KeyboardEvent) { | ||
const { isOpen, panelElement, closePopover } = this.args; | ||
const eventKey = normalizeKey(event); | ||
|
||
switch (eventKey) { | ||
case 'Escape': | ||
if (!isOpen) return; | ||
if (!panelElement) return; | ||
if (!panelElement.contains(document.activeElement)) return; | ||
|
||
event.preventDefault(); | ||
event.stopPropagation(); | ||
closePopover(); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{{! template-lint-disable no-down-event-binding }} | ||
<button | ||
id={{@triggerGuid}} | ||
type="button" | ||
aria-haspopup="true" | ||
aria-controls={{if @isOpen @panelGuid}} | ||
aria-expanded={{@isOpen}} | ||
...attributes | ||
{{@triggerRef}} | ||
{{on "click" @togglePopover}} | ||
{{on "keydown" this.handleKeydown}} | ||
> | ||
{{yield}} | ||
</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { action } from '@ember/object'; | ||
import { next } from '@ember/runloop'; | ||
import Component from '@glimmer/component'; | ||
|
||
import { normalizeKey } from '@showbie/backpack-ember/utils/normalize-keycode'; | ||
|
||
interface TriggerArgs { | ||
triggerGuid: string; | ||
isOpen: boolean; | ||
openPopover: () => void; | ||
closePopover: () => void; | ||
} | ||
|
||
export default class Trigger extends Component<TriggerArgs> { | ||
@action | ||
handleKeydown(event: KeyboardEvent) { | ||
// @ts-expect-error -- `disabled` does exist on evt.target | ||
if (event.target?.disabled) return; | ||
|
||
const eventKey = normalizeKey(event); | ||
switch (eventKey) { | ||
case 'Space': | ||
case 'Enter': | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
|
||
if (this.args.isOpen && eventKey === 'Enter') { | ||
this.args.closePopover(); | ||
} else { | ||
this.args.openPopover(); | ||
next(() => { | ||
// Focus first item? | ||
}); | ||
} | ||
break; | ||
|
||
case 'Escape': | ||
if (!this.args.isOpen) return; | ||
|
||
event.preventDefault(); | ||
event.stopPropagation(); | ||
this.args.closePopover(); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
type KeyboardEventValues = Pick<KeyboardEvent, 'key' | 'keyCode'>; | ||
|
||
/** | ||
* Normalizes the 'key' property of a KeyboardEvent in IE/Edge. | ||
*/ | ||
export function normalizeKey({ key, keyCode }: KeyboardEventValues) { | ||
if (keyCode >= 37 && keyCode <= 40 && key.indexOf('Arrow') !== 0) { | ||
return `Arrow${key}`; | ||
} | ||
if (keyCode === 27) { | ||
return 'Escape'; | ||
} | ||
return key; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '@showbie/backpack-ember/components/popover'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '@showbie/backpack-ember/components/popover/panel'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '@showbie/backpack-ember/components/popover/trigger'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { normalizeKey } from '@showbie/backpack-ember/utils/normalize-keycode'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This internal action calls the
closePopover
argument function, which in turn updates the value ofisOpen
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this affects anything that you are working with right now but shouldn't this be
this.args.closePopover()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, it's destructured off of
this.args
a few lines up, but I'll see if it makes a difference...NARRATOR: It didn't.