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

Lightbox: work in progress #8

Merged
merged 3 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
116 changes: 114 additions & 2 deletions index-react.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">

<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="http://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/react-router-dom/umd/react-router-dom.js"></script>
<script src="https://unpkg.com/babel-standalone/babel.min.js"></script>
<script type="module">
import {AmpImg} from './src/amp-react-img.js';
import {AmpCarouselHooks} from './src/amp-react-carousel-hooks.js';
import {AmpLightbox} from './src/amp-react-lightbox.js';
import {AmpSize} from './src/amp-react-size.js';
import {AmpYoutubeHooks} from './src/amp-react-youtube-hooks.js';
window.AmpImg = AmpImg;
window.AmpCarousel = AmpCarouselHooks;
window.AmpLightbox = AmpLightbox;
window.AmpSize = AmpSize;
window.AmpYoutubeHooks = AmpYoutubeHooks;
</script>
Expand All @@ -28,11 +31,75 @@
class App extends React.Component {
state = {
slides: [0, 1, 2, 3, 4, 5],
lightbox1Open: false,
};

constructor(props) {
super(props);
this.backButtonService = new BackButtonServiceFake();
this.buttonRef = React.createRef();
}

render() {
const lightbox1BlockedMessage =
'This lightbox has unsaved changes. Are you sure you want to close it?';
return (
<main>
<ReactRouterDOM.BrowserRouter>
<ReactRouterDOM.Route>
{props => (
<AmpLightbox
history={props.history}
routerProps={props}
id="lightbox1"
open={this.state.lightbox1Open}
onClose={() => this.setState({lightbox1Open: false})}
closeConfirm={this.state.lightbox1Blocked && lightbox1BlockedMessage}
deps={{backButton: this.backButtonService}}
// TBD: no way to inject ref.current instead? No re-render
// on hydration...
transitionAnchor={this.buttonRef}
>
<div>This is a lightbox</div>

<AmpImg
id="l1_img1"
alt="bindable"
src="https://via.placeholder.com/90x90.png?text=lightbox1-1"
style={{width: '90px', height: '90px'}}>
</AmpImg>

<form
onChange={() => this.setState({lightbox1Blocked: true})}
onReset={() => this.setState({lightbox1Blocked: false})}
>
<textarea rows="5" cols="10"></textarea>
<input type="checkbox"/>
<input type="reset" value="Reset"/>
</form>

<div style={{height: '500vh'}}>scroll down...</div>

<AmpImg
id="l1_img2"
alt="bindable"
src="https://via.placeholder.com/90x90.png?text=lightbox1-2"
style={{width: '90px', height: '90px'}}>
</AmpImg>
</AmpLightbox>
)}
</ReactRouterDOM.Route>
</ReactRouterDOM.BrowserRouter>

<div>
<button
ref={this.buttonRef}
onClick={() => this.setState({lightbox1Open: true})}>
Show lightbox
</button>
</div>


<h1>amp-react-img</h1>
<AmpImg
id="first"
Expand Down Expand Up @@ -92,8 +159,53 @@ <h1>amp-react-img</h1>
}
}

ReactDOM.render(<App/>, document.getElementById('app'))

// TODO: redo this service via Router.history object.
class BackButtonServiceFake {
/** @return BackButtonMarker */
push() {
return new BackButtonMarkerFake();
}
}

class BackButtonMarkerFake {
constructor() {
this.closed = false;
/** @type {?function()} */
this.onPop = null;

/** @private {boolean} */
this.popped_ = false;
this.popHandler_ = () => {
this.popped_ = true;
this.pop();
};
window.addEventListener('popstate', this.popHandler_);
history.pushState({index: 1}, '');
}

pop() {
if (this.closed) {
return;
}
this.closed = true;
if (this.onPop) {
this.onPop();
this.onPop = null;
}
if (!this.popped_) {
this.popped_ = true;
history.go(-1);
}
if (this.popHandler_) {
window.removeEventListener('popstate', this.popHandler_);
this.popHandler_ = null;
}
}
}


ReactDOM.render(<App/>, document.getElementById('app'))
</script>
</body>
</html>
36 changes: 36 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,12 @@

<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="http://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/prop-types/prop-types.js"></script>
<script src="https://unpkg.com/react-router/umd/react-router.js"></script>
<script type="module" src="./src/amp-react-img.js"></script>
<script type="module" src="./src/amp-react-carousel.js"></script>
<script type="module" src="./src/amp-react-carousel-hooks.js"></script>
<script type="module" src="./src/amp-react-lightbox.js"></script>
<script type="module" src="./src/amp-react-size.js"></script>
<script type="module" src="./src/amp-react-youtube.js"></script>
<script type="module" src="./src/amp-react-youtube-hooks.js"></script>
Expand All @@ -200,6 +203,39 @@
</head>
<body>

<amp-react-lightbox id="lightbox1" hidden transition-anchor="button1">
<div>This is a lightbox</div>

<amp-react-img
id="l1_img1"
alt="bindable"
src="https://via.placeholder.com/90x90.png?text=lightbox1-1"
width=90
height=90></amp-react-img>

<form
onchange="console.log('form changed')"
onreset="console.log('form reset')">
<input value="">
<textarea rows="5" cols="50"></textarea>
<input type="checkbox">
<input type="reset" value="Reset">
</form>

<div style="height: 500vh">scroll down...</div>

<amp-react-img
id="l1_img2"
alt="bindable"
src="https://via.placeholder.com/90x90.png?text=lightbox1-2"
width=90
height=90></amp-react-img>
</amp-react-lightbox>

<div>
<button id="button1" onclick="lightbox1.activate()">Show lightbox</button>
</div>

<h1>amp-react-img</h1>
<amp-react-img
id="first"
Expand Down
8 changes: 8 additions & 0 deletions src/amp-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ export default function AmpElementFactory(BaseElement) {
}
}

activate() {
this.executeAction({action: 'activate'});
}

executeAction(invocation) {
this.implementation_.executeAction(invocation);
}

handleIntersectionObserver_(records) {
const last = records.length
? records[records.length - 1]
Expand Down