-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Adding A Modal Component #1369
Open
aryanpingle
wants to merge
23
commits into
GoogleChromeLabs:dev
Choose a base branch
from
aryanpingle:feat--modal
base: dev
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.
Open
Adding A Modal Component #1369
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0159f23
Add modal skeleton
aryanpingle 7dd67bc
Improve the modal component
aryanpingle 2b4d893
Change Modal directory + color scheme
aryanpingle 89f94f7
Bump TS version for `<dialog>` support
aryanpingle 4e98cab
Refactor modal to use `<dialog>`
aryanpingle d1337d2
Modal - use VNode icon and content
aryanpingle e9e2c30
Replace Context with shared functions
aryanpingle e1875a5
Remove singleton pattern from modal
aryanpingle b66ec29
Add ModalHint
aryanpingle fefe3d3
Basic `ModalHint` demo
aryanpingle eea1802
Add `animateTo` function
aryanpingle dc5b15c
Switch to Web Animations API
aryanpingle d766ecb
Catch animation errors for ::backdrop
aryanpingle 3e4e8f2
Apply suggestions from code review
aryanpingle 6adf4be
Switch from eased to linear animations
aryanpingle 5cdf594
Remove obsolete code
aryanpingle 36c646e
Refactoring
aryanpingle a25f093
Add RoundedCrossIcon to icons
aryanpingle d9391d0
Refactor ModalHint
aryanpingle 8511b8f
Improved icons
aryanpingle 313d06f
Use `animateFrom` to show modal
aryanpingle 84c19ca
(REMOVE) Update modal demo
aryanpingle 365021c
Compress SVGs
aryanpingle 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,127 @@ | ||
import { h, Component, VNode, Fragment } from 'preact'; | ||
import * as style from './style.css'; | ||
import 'add-css:./style.css'; | ||
import { linkRef } from 'shared/prerendered-app/util'; | ||
import { cleanSet } from '../util/clean-modify'; | ||
|
||
interface Props {} | ||
|
||
export interface ModalMessage { | ||
icon: VNode; | ||
title: string; | ||
content: VNode; | ||
} | ||
|
||
interface State { | ||
message: ModalMessage; | ||
shown: boolean; | ||
} | ||
|
||
export default class Modal extends Component<Props, State> { | ||
state: State = { | ||
message: { | ||
icon: <svg></svg>, | ||
title: 'default', | ||
content: <Fragment></Fragment>, | ||
}, | ||
aryanpingle marked this conversation as resolved.
Show resolved
Hide resolved
aryanpingle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
shown: false, | ||
}; | ||
|
||
private dialogElement!: HTMLDialogElement; | ||
static modalInstance?: Modal | undefined; | ||
aryanpingle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
componentDidMount() { | ||
// Once a transition ends, check if the modal should be closed (not just hidden) | ||
// dialog.close() instantly hides the modal, so we call it AFTER fading it out i.e. on transition end | ||
this.dialogElement.addEventListener( | ||
'transitionend', | ||
this._closeOnTransitionEnd.bind(this), | ||
); | ||
aryanpingle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.dialogElement.setAttribute('inert', 'enabled'); | ||
aryanpingle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Modal.modalInstance = this; | ||
} | ||
|
||
private _closeOnTransitionEnd() { | ||
aryanpingle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// If modal does not exist | ||
// Or if it's not being closed at the moment | ||
if ( | ||
!this.dialogElement || | ||
!this.dialogElement.classList.contains(style.modalClosing) | ||
) | ||
return; | ||
|
||
this.dialogElement.close(); | ||
this.dialogElement.classList.remove(style.modalClosing); | ||
this.dialogElement.setAttribute('inert', 'enabled'); | ||
} | ||
|
||
static showModal(message: ModalMessage) { | ||
Modal.modalInstance?._showModal(message); | ||
} | ||
|
||
static hideModal() { | ||
Modal.modalInstance?._hideModal(); | ||
} | ||
|
||
private _showModal(message: ModalMessage) { | ||
if (!this.dialogElement) throw Error('Modal missing'); | ||
|
||
this.setState({ | ||
message: message, | ||
shown: true, | ||
}); | ||
|
||
// Actually show the modal | ||
this.dialogElement.removeAttribute('inert'); | ||
this.dialogElement.showModal(); | ||
} | ||
|
||
private _hideModal() { | ||
if (!this.dialogElement || !this.dialogElement.open) | ||
throw Error('Modal missing / hidden'); | ||
|
||
// Make the modal fade out | ||
this.dialogElement.classList.add(style.modalClosing); | ||
|
||
this.setState(cleanSet(this.state, 'shown', false)); | ||
} | ||
|
||
private _onKeyDown(e: KeyboardEvent) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Rename the arg to |
||
// Default behaviour of <dialog> closes it instantly when you press Esc | ||
// So we hijack it to smoothly hide the modal | ||
if (e.key === 'Escape') { | ||
this._hideModal(); | ||
e.preventDefault(); | ||
e.stopImmediatePropagation(); | ||
} | ||
} | ||
|
||
render({}: Props, { message, shown }: State) { | ||
return ( | ||
<dialog | ||
ref={linkRef(this, 'dialogElement')} | ||
onKeyDown={(e) => this._onKeyDown(e)} | ||
aryanpingle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
> | ||
<header class={style.header}> | ||
<span class={style.modalIcon}>{message.icon}</span> | ||
<span class={style.modalTitle}>{message.title}</span> | ||
<button class={style.closeButton} onClick={() => this._hideModal()}> | ||
<svg viewBox="0 0 480 480" fill="currentColor"> | ||
<path | ||
d="M119.356 120L361 361M360.644 120L119 361" | ||
stroke="#fff" | ||
stroke-width="37" | ||
stroke-linecap="round" | ||
/> | ||
</svg> | ||
</button> | ||
</header> | ||
<div class={style.contentContainer}> | ||
<article class={style.content}>{message.content}</article> | ||
</div> | ||
<footer class={style.footer}></footer> | ||
</dialog> | ||
); | ||
} | ||
} |
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.
I think we decided that the content of the model should be the children of the modal?