-
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
✨ Bento Lightbox Gallery #32008
Merged
Merged
✨ Bento Lightbox Gallery #32008
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
1aed0e1
Prototype
caroqliu 5e43aec
Merge branch 'master' into bento-lightbox-gallery
caroqliu ff3a400
Update with deeply nested example
caroqliu 296e7a4
Preact skeleton
caroqliu 2941bc2
cloneElement takes a children param
caroqliu 490e8e5
Merge branch 'master' into bento-lightbox-gallery
caroqliu e536acd
Adopt new file structure
caroqliu d627d32
Only use WithLightbox
caroqliu ff2394b
Make WithLightbox more customizable
caroqliu 25536d3
Lightbox content should support scrolling
caroqliu f94b78c
Support lightboxing BaseCarousel
caroqliu 0fd6a24
Fix types
caroqliu 752fea6
Merge branch 'master' into bento-lightbox-gallery
caroqliu 86cf9ff
Specify cross-component file dependencies
caroqliu f5fc8f7
Dedupe contentRef and lightboxRef
caroqliu d453e24
With lightbox does not need a ref
caroqliu bdec869
Merge branch 'master' into bento-lightbox-gallery
caroqliu e6a3baf
Fix a11y attributes
caroqliu 708771e
Don't render elements until the LightboxGallery opens
caroqliu b739ce3
Don't take thumbnailSrc for lightbox
caroqliu 3926d94
Split up files
caroqliu 97b3256
LightboxGallery -> LightboxGalleryProvider
caroqliu 9fc9c42
Update dependency list
caroqliu df95ff4
LightboxGalleryProvider render prop
caroqliu b1ab988
Remove key
caroqliu 1e5b26a
Rename autoLightbox as enableActivation
caroqliu 0145e2b
Update type definitions
caroqliu e99044e
Always render <Comp> layer
caroqliu c9cc0dd
Export directly
caroqliu 83e6a02
Merge branch 'master' into bento-lightbox-gallery
caroqliu d976861
lightbox.js moved to component.js
caroqliu File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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,92 @@ | ||
/** | ||
* Copyright 2021 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 * as Preact from '../../../../src/preact'; | ||
// import {LightboxGallery} from '../lightbox-gallery'; | ||
import {Lightbox} from './../../../amp-lightbox/1.0/lightbox'; | ||
import {withA11y} from '@storybook/addon-a11y'; | ||
import {withKnobs} from '@storybook/addon-knobs'; | ||
|
||
export default { | ||
title: 'LightboxGallery', | ||
// component: LightboxGallery, | ||
decorators: [withKnobs, withA11y], | ||
}; | ||
|
||
const LightboxGalleryContext = Preact.createContext({}); | ||
|
||
/** | ||
* @param {!Object} props | ||
* @return {*} | ||
*/ | ||
function WithLightbox({children}) { | ||
const {open, pushChild} = Preact.useContext(LightboxGalleryContext); | ||
Preact.useLayoutEffect(() => { | ||
pushChild(children); | ||
}, []); | ||
return <div onClick={() => open()}>{children}</div>; | ||
} | ||
|
||
/** | ||
* @param {!Object} props | ||
* @return {*} | ||
*/ | ||
function LightboxGallery({children}) { | ||
const lightboxRef = Preact.useRef(null); | ||
const lightboxElements = Preact.useRef([]); | ||
const pushChild = (el) => lightboxElements.current.push(el); | ||
const context = {pushChild, open: () => lightboxRef.current.open()}; | ||
return ( | ||
<> | ||
<Lightbox ref={lightboxRef}> | ||
<div onClick={() => lightboxRef.current.close()}>close lightbox</div> | ||
<div>{lightboxElements.current}</div> | ||
</Lightbox> | ||
<LightboxGalleryContext.Provider value={context}> | ||
{Preact.toChildArray(children).map((child) => | ||
child.props.lightbox ? <WithLightbox>{child}</WithLightbox> : child | ||
)} | ||
</LightboxGalleryContext.Provider> | ||
</> | ||
); | ||
} | ||
|
||
export const _default = () => { | ||
return ( | ||
<LightboxGallery> | ||
<style>{` | ||
img { | ||
width: 240px; | ||
height: 160px; | ||
} | ||
`}</style> | ||
<img | ||
lightbox | ||
src="https://images.unsplash.com/photo-1583511655857-d19b40a7a54e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1498&q=80" | ||
/> | ||
<p>abc</p> | ||
<img | ||
lightbox | ||
src="https://images.unsplash.com/photo-1583511666407-5f06533f2113?ixlib=rb-1.2.1&auto=format&fit=crop&w=1498&q=80" | ||
/> | ||
<p>abc</p> | ||
<img | ||
lightbox | ||
src="https://images.unsplash.com/photo-1599839575945-a9e5af0c3fa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjQwMzA0fQ&auto=format&fit=crop&w=1498&q=80" | ||
/> | ||
</LightboxGallery> | ||
); | ||
}; |
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.
Ok, I think I understood your process here. And it makes sense to me. One nuance - it cannot traverse nested children. But we could enable it with making
WithLightbox
public. E.g. the user code could look like this:Somewhere once:
And elsewhere:
This might be pretty clean from Preact point of view. Some of our components could even call
WithLightbox
internally.In AMP we could enable this somehow too:
LightboxProvider
attachment is very easy for us now - all it needs to do is to propagate context. We do this today withInlineGallery
.WithLightbox
would be a bit harder. But I think we can figure it out with "detached" mode. Or maybe we'd just always require related components to callWithLightbox
internally for now.The important question here: do we currently clone DOM elements to put them inside the lightbox? Or do we reparent them? Or just position? This is important, because if we clone/reposition, then in React that'd probably be taken care of by portalling, which we haven't worked with yet here, but could open some doors for us.
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.
We can get nested children if we are willing to clone elements, but this is costly and somewhat hacky to drill it down like this:
To your point about portals - 0.1 clones and cleans lightboxable DOM elements:
amphtml/extensions/amp-lightbox-gallery/0.1/amp-lightbox-gallery.js
Lines 269 to 290 in ea2e7f6
Edit: Discussed portals offline and prefer taking
render
prop for inside the lightbox.