Skip to content

Instantly share code, notes, and snippets.

@avishwakarma
Created May 9, 2019 16:40
Show Gist options
  • Save avishwakarma/64a55f86b4b8b3d0e050af8da2a448ec to your computer and use it in GitHub Desktop.
Save avishwakarma/64a55f86b4b8b3d0e050af8da2a448ec to your computer and use it in GitHub Desktop.
Portal example
// Adding some styles with transitions
const style = document.createElement('style');
const initialScale = 0.4;
style.innerHTML = `
portal {
position:fixed;
width: 100%;
height: 100%;
opacity: 0;
transition:
transform 0.4s,
bottom 0.7s,
left 0.7s,
opacity 1.0s;
box-shadow: 0 0 20px 10px #999;
transform: scale(${initialScale});
bottom: calc(20px + 50% * ${initialScale} - 50%);
left: calc(20px + 50% * ${initialScale} - 50%);
z-index: 10000;
}
.portal-reveal {
transform: scale(1.0);
bottom: 0px;
left: 0px;
}
.fade-in {
opacity: 1.0;
}
`;
const portal = document.createElement('portal');
// Let’s navigate into the WICG Portals spec page
portal.src = 'https://wicg.github.io/portals/';
portal.addEventListener('click', evt => {
// Animate the portal once user interacts
portal.classList.add('portal-reveal');
});
portal.addEventListener('transitionend', evt => {
if (evt.propertyName == 'bottom') {
// Activate the portal once the transition has completed
portal.activate();
}
});
document.body.append(style, portal);
// Waiting for the page to load.
// using setTimeout is a suboptimal way and it’s best to fade-in
// when receiving a load complete message from the portal via postMessage
setTimeout(_ => portal.classList.add('fade-in'), 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment