-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Sidebar - Basic Open and close functionality #2795
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,8 @@ | |
import {CSS} from '../../../build/amp-sidebar-0.1.css'; | ||
import {Layout} from '../../../src/layout'; | ||
import {isExperimentOn} from '../../../src/experiments'; | ||
|
||
import {dev} from '../../../src/log'; | ||
import {setStyles} from '../../../src/style'; | ||
|
||
/** @const */ | ||
const EXPERIMENT = 'amp-sidebar'; | ||
|
@@ -41,10 +42,148 @@ export class AmpSidebar extends AMP.BaseElement { | |
/** @const @private {boolean} */ | ||
this.isExperimentOn_ = isExperimentOn(this.getWin(), EXPERIMENT); | ||
|
||
/** @private @const {!Window} */ | ||
this.win_ = this.getWin(); | ||
|
||
/** @private @const {!Document} */ | ||
this.document_ = this.win_.document; | ||
|
||
/** @private @const {!Element} */ | ||
this.documentElement_ = this.document_.documentElement; | ||
|
||
/** @private @const {string} */ | ||
this.side_ = this.element.getAttribute('side'); | ||
|
||
/** @private @const {!Viewport} */ | ||
this.viewport_ = this.getViewport(); | ||
|
||
/** @private @const {!Element} */ | ||
this.maskElement_ = false; | ||
|
||
if (!this.isExperimentOn_) { | ||
dev.warn(TAG, `Experiment ${EXPERIMENT} disabled`); | ||
return; | ||
} | ||
|
||
if (this.side_ != 'left' && this.side_ != 'right') { | ||
const pageDir = | ||
this.document_.body.getAttribute('dir') || | ||
this.documentElement_.getAttribute('dir') || | ||
'ltr'; | ||
this.side_ = (pageDir == 'rtl') ? 'right' : 'left'; | ||
this.element.setAttribute('side', this.side_); | ||
} | ||
|
||
if (this.isOpen_()) { | ||
this.open_(); | ||
} else { | ||
this.element.setAttribute('aria-hidden', 'true'); | ||
} | ||
|
||
this.documentElement_.addEventListener('keydown', event => { | ||
// Close sidebar on ESC. | ||
if (event.keyCode == 27) { | ||
this.close_(); | ||
} | ||
}); | ||
|
||
//TODO (skrish, #2712) Add history support on back button. | ||
this.registerAction('toggle', this.toggle_.bind(this)); | ||
this.registerAction('open', this.open_.bind(this)); | ||
this.registerAction('close', this.close_.bind(this)); | ||
} | ||
|
||
/** | ||
* Returns true if the sidebar is opened. | ||
* @returns {boolean} | ||
* @private | ||
*/ | ||
isOpen_() { | ||
return this.element.hasAttribute('open'); | ||
} | ||
|
||
/** @override */ | ||
activate() { | ||
this.open_(); | ||
} | ||
|
||
|
||
/** | ||
* Toggles the open/close state of the sidebar. | ||
* @private | ||
*/ | ||
toggle_() { | ||
if (this.isOpen_()) { | ||
this.close_(); | ||
} else { | ||
this.open_(); | ||
} | ||
} | ||
|
||
/** | ||
* Reveals the sidebar. | ||
* @private | ||
*/ | ||
open_() { | ||
this.viewport_.disableTouchZoom(); | ||
this.mutateElement(() => { | ||
this.viewport_.addToFixedLayer(this.element); | ||
setStyles(this.element, { | ||
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. Please use 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. in this case the sidebar needs to be block; since it is a custom element. we would need to make it block |
||
'display': 'block', | ||
}); | ||
this.openMask_(); | ||
this.element.setAttribute('open', ''); | ||
this.element.setAttribute('aria-hidden', 'false'); | ||
}); | ||
} | ||
|
||
/** | ||
* Hides the sidebar. | ||
* @private | ||
*/ | ||
close_() { | ||
this.viewport_.restoreOriginalTouchZoom(); | ||
this.mutateElement(() => { | ||
this.closeMask_(); | ||
this.element.removeAttribute('open'); | ||
this.element.setAttribute('aria-hidden', 'true'); | ||
setStyles(this.element, { | ||
'display': 'none', | ||
}); | ||
this.viewport_.removeFromFixedLayer(this.element); | ||
}); | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
openMask_() { | ||
if (!this.maskElement_) { | ||
const mask = this.document_.createElement('div'); | ||
mask.classList.add('-amp-sidebar-mask'); | ||
mask.addEventListener('click', () => { | ||
this.close_(); | ||
}); | ||
this.element.parentNode.appendChild(mask); | ||
mask.addEventListener('touchmove', e => { | ||
e.preventDefault(); | ||
}); | ||
this.maskElement_ = mask; | ||
} | ||
setStyles(this.maskElement_, { | ||
'display': 'block', | ||
}); | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
closeMask_() { | ||
if (this.maskElement_) { | ||
setStyles(this.maskElement_, { | ||
'display': 'none', | ||
}); | ||
} | ||
} | ||
} | ||
|
||
|
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.
Didn't we decide to remove
!important
here?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.
removed on the top property