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

amp-sidebar #2461

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions extensions/amp-sidebar/0.1/amp-sidebar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

amp-sidebar {
position: fixed !important;
top: 0 !important;
Copy link
Contributor

Choose a reason for hiding this comment

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

This will be an issue - we have to be flexible with top positioning to accommodate page's padding.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, some people like to align it under their header.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They could always have a margin-top

Copy link
Contributor

Choose a reason for hiding this comment

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

It's possible. Do you think it'll be clear enough? This will also conflict slightly with your height: 100vh;. I think the only way to work it around would be by using height: calc(100vh - {top offset value})

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 have added an offset div to be inserted as the first element when there is a viewport.paddingTop- that should take care of this

max-height: 100vh !important;
height: 100vh;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add box-sizing: border-box right away for this.

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 dont think it is required with the latest changes.

max-width: 80vw !important;
background-color: #efefef;
min-width: 30vw !important;
overflow-x: hidden !important;
overflow-y: auto !important;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please create three bugs, assign to yourself and proceed at the earliest convenience (they will be launch blocking on this component):

  1. Possible integration with FixedLayer needed. This can be tested on iOS by scrolling and seeing if the position:fixed layout misbehaves. Also, other elements processed via FixedLayer can conflict with your logic, e.g. by being displayed above this menu.
  2. Check double-scrolling issue. Here, the test is on both iOS and Android. When scrolling the menu content, the main content should not scroll and vice-versa.
  3. Check the scrolling restoration issues on iOS and Android. E.g. open menu, scroll it and close. The main content should be unaffected by this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tested and Fixed - thanks for the pointers

z-index: 9999 !important;
-webkit-overflow-scrolling: touch;
}

amp-sidebar[side] {
display: block !important;
}

amp-sidebar[side="left"] {
left: 0 !important;
transform: translateX(-80vw) !important;
}

amp-sidebar[side="right"] {
right: 0 !important;
transform: translateX(80vw) !important;
}

amp-sidebar[side][open] {
transform: translateX(0vw) !important;
}

amp-sidebar[open],
.-amp-sidebar-closed {
transition: transform 0.5s ease;
}

.-amp-sidebar-mask {
position: fixed !important;
top: 0 !important;
left: 0 !important;
width: 100vw !important;
height: 100vh !important;
opacity: 0.2;
/*Prevent someone from making this a full-sceen image*/
background-image: none !important;
background-color: #000;
z-index: 9998 !important;
display: none;
}
284 changes: 284 additions & 0 deletions extensions/amp-sidebar/0.1/amp-sidebar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {CSS} from '../../../build/amp-sidebar-0.1.css';
import {Layout} from '../../../src/layout';
import {assert} from '../../../src/asserts';
import {isExperimentOn} from '../../../src/experiments';
import {dev} from '../../../src/log';
import {platform} from '../../../src/platform';
import {setStyles} from '../../../src/style';


/** @const */
const EXPERIMENT = 'amp-sidebar';

/** @const */
const TAG = 'AmpSidebar';

/** @const */
const WHITELIST_ = ['AMP-ACCORDION', 'AMP-FIT-TEXT', 'AMP-IMG'];

/** @const */
const IOS_SAFARI_BOTTOMBAR_HEIGHT = '10vh';

export class AmpSidebar extends AMP.BaseElement {

/** @override */
isLayoutSupported(layout) {
return layout == Layout.NOLAYOUT;
}

/** @override */
isReadyToBuild() {
return false;
}

/** @override */
buildCallback() {
/** @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;

/** @private @const {boolean} */
this.isPaddingAdjusted_ = false;

/** @private @const {boolean} */
this.isIosSafari_ = platform.isIos() && platform.isSafari();

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.isExperimentOn_) {
dev.warn(TAG, `Experiment ${EXPERIMENT} disabled`);
return;
}
if (!this.checkWhitelist_()) {
return;
}

if (this.isIosSafari_) {
this.fixIosElasticScrollLeak_();
}

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));
}

/** @override */
activate() {
this.open_();
}

/**
* @private
*/
adjustPadding_() {
const viewerPaddingTop = this.viewport_.getPaddingTop();
if (viewerPaddingTop) {
// viewerPaddingTop exists when AMP page is rendered inside search
// carousel.
const div = this.document_.createElement('div');
setStyles(div, {
'height': viewerPaddingTop + 'px',
'width': '100%',
});
const firstChild = this.element.firstChild;
this.element.insertBefore(div, firstChild);
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 hard to guarantee to work - this will skew many possible design situations. The best way is still to adjust top position. If for some reason this is not good - we can explore updating border-top. But we need to have relative position's reference point to be in the right place.

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 already tried adding padding - and it does not seem to work for the bottom padding on IOS and adding padding/border will need vsync to compute existing style.

Adding a dummy child always (seems to) work. What do you think will not work here. I'm probably not seeing the obvious :)

}
if (this.isIosSafari_) {
//Compensate for IOS safari bottom navbar.
const div = this.document_.createElement('div');
setStyles(div, {
'height': IOS_SAFARI_BOTTOMBAR_HEIGHT,
'width': '100%',
'background-color': 'transparent',
});
this.element.appendChild(div);
}
this.isPaddingAdjusted_ = true;
}

/**
* Toggles the open/close state of the sidebar.
* @private
*/
toggle_() {
if (this.isOpen_()) {
this.close_();
} else {
this.open_();
}
}

/**
* Returns true if the sidebar is opened.
* @returns {boolean}
* @private
*/
isOpen_() {
return this.element.hasAttribute('open');
}

/**
* Reveals the sidebar.
* @private
*/
open_() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please also support activate method and just call this method from it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

this.viewport_.disableTouchZoom();
if (!this.isPaddingAdjusted_) {
this.adjustPadding_();
}
this.mutateElement(() => {
this.viewport_.addToFixedLayer(this.element);
this.openMask_();
this.element.setAttribute('open', '');
this.element.classList.remove('-amp-sidebar-closed');
this.element.setAttribute('aria-hidden', 'false');
this.element./*REVIEW*/scrollTop = 1;
});
}

/**
* Hides the sidebar.
* @private
*/
close_() {
this.viewport_.restoreOriginalTouchZoom();
this.mutateElement(() => {
this.closeMask_()
this.element.removeAttribute('open');
this.element.classList.add('-amp-sidebar-closed');
this.element.setAttribute('aria-hidden', 'true');
this.viewport_.removeFromFixedLayer(this.element);
});
}

/**
* Checks if the sidebar only has the whitlisted custom amp- elements.
* @returns {boolean} True when only whitelited elements are present.
* @private
*/
checkWhitelist_() {
const elements = this.element.getElementsByTagName('*');
Copy link
Contributor

Choose a reason for hiding this comment

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

What exactly don't we want in the menu again? I still don't understand the whitelist, but it's rather expensive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ads - iframes , instagram, FB , videos , Youtube etc ... its a long list.

I would assume that there will be a limited no of elements on the sidebar (considering the space available). We could possibly remove this if/when this gets implemented in validation.

let i = elements.length - 1;
while (i >= 0) {
const tagName = elements[i].tagName;
if (tagName.indexOf('AMP-') == 0) {
const isWhiteListed = assert(
WHITELIST_.indexOf(tagName) >= 0,
'%s can only contain the following custom tags: %s',
this.element, WHITELIST_);
if (!isWhiteListed) {
return false;
}
}
i--;
}
return true;
}

/**
* @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',
});
}
}

/**
* @private
*/
fixIosElasticScrollLeak_() {
this.element.addEventListener('scroll', e => {
if (this.isOpen_()) {
if (this.element./*REVIEW*/scrollTop < 1) {
this.element./*REVIEW*/scrollTop = 1;
e.preventDefault();
} else if (this.element./*REVIEW*/scrollHeight ==
this.element./*REVIEW*/scrollTop +
this.element./*REVIEW*/offsetHeight) {
this.element./*REVIEW*/scrollTop =
this.element./*REVIEW*/scrollTop - 1;
e.preventDefault();
}
}
});
}
}

AMP.registerElement('amp-sidebar', AmpSidebar, CSS);
Loading