Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Prototype * Update with deeply nested example * Preact skeleton * cloneElement takes a children param * Adopt new file structure * Only use WithLightbox * Make WithLightbox more customizable * Lightbox content should support scrolling * Support lightboxing BaseCarousel * Fix types * Specify cross-component file dependencies * Dedupe contentRef and lightboxRef * With lightbox does not need a ref * Fix a11y attributes * Don't render elements until the LightboxGallery opens * Don't take thumbnailSrc for lightbox * Split up files * LightboxGallery -> LightboxGalleryProvider * Update dependency list * LightboxGalleryProvider render prop * Remove key * Rename autoLightbox as enableActivation * Update type definitions * Always render <Comp> layer * Export directly * lightbox.js moved to component.js
- Loading branch information
Showing
11 changed files
with
383 additions
and
3 deletions.
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
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
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
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
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,18 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
export {LightboxGalleryProvider} from './provider'; | ||
export {WithLightbox} from './consumer'; |
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,48 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
/** @externs */ | ||
|
||
/** @const */ | ||
var LightboxGalleryDef = {}; | ||
|
||
/** | ||
* @typedef {{ | ||
* children: (PreactDef.Renderable), | ||
* render: (function():PreactDef.Renderable|undefined), | ||
* }} | ||
*/ | ||
LightboxGalleryDef.Props; | ||
|
||
/** | ||
* @typedef {{ | ||
* as: (string|undefined), | ||
* children: (!PreactDef.Renderable), | ||
* enableActivation: (boolean|undefined), | ||
* onClick: (function(Event)|undefined) | ||
* render: (function():PreactDef.Renderable), | ||
* }} | ||
*/ | ||
LightboxGalleryDef.WithLightboxProps; | ||
|
||
/** | ||
* @typedef {{ | ||
* deregister: (function(string):undefined), | ||
* register: (function(string, Element):undefined), | ||
* open: (function:undefined), | ||
* }} | ||
*/ | ||
LightboxGalleryDef.ContextProps; |
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,73 @@ | ||
/** | ||
* 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 {LightboxGalleryContext} from './context'; | ||
import {sequentialIdGenerator} from '../../../src/utils/id-generator'; | ||
import { | ||
useContext, | ||
useLayoutEffect, | ||
useMemo, | ||
useState, | ||
} from '../../../src/preact'; | ||
|
||
const generateLightboxItemKey = sequentialIdGenerator(); | ||
|
||
const DEFAULT_ARIA_LABEL = 'Open content in a lightbox view.'; | ||
const DEFAULT_ACTIVATION_PROPS = { | ||
'aria-label': DEFAULT_ARIA_LABEL, | ||
role: 'button', | ||
tabIndex: '0', | ||
}; | ||
|
||
/** | ||
* @param {!LightboxGalleryDef.WithLightboxProps} props | ||
* @return {PreactDef.Renderable} | ||
*/ | ||
export function WithLightbox({ | ||
as: Comp = 'div', | ||
children, | ||
enableActivation = true, | ||
onClick: customOnClick, | ||
render = () => children, | ||
...rest | ||
}) { | ||
const [genKey] = useState(generateLightboxItemKey); | ||
const {open, register, deregister} = useContext(LightboxGalleryContext); | ||
useLayoutEffect(() => { | ||
register(genKey, render); | ||
return () => deregister(genKey); | ||
}, [genKey, deregister, register, render]); | ||
|
||
const activationProps = useMemo( | ||
() => | ||
enableActivation && { | ||
...DEFAULT_ACTIVATION_PROPS, | ||
onClick: () => { | ||
if (customOnClick) { | ||
customOnClick(); | ||
} | ||
open(); | ||
}, | ||
}, | ||
[customOnClick, enableActivation, open] | ||
); | ||
return ( | ||
<Comp {...activationProps} {...rest}> | ||
{children} | ||
</Comp> | ||
); | ||
} |
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,25 @@ | ||
/** | ||
* 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 {createContext} from '../../../src/preact'; | ||
|
||
const LightboxGalleryContext = createContext( | ||
/** @type {LightboxGalleryDef.ContextProps} */ ({ | ||
deregister: () => {}, | ||
register: () => {}, | ||
open: () => {}, | ||
}) | ||
); | ||
export {LightboxGalleryContext}; |
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,72 @@ | ||
/** | ||
* 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 {Lightbox} from './../../amp-lightbox/1.0/component'; | ||
import {LightboxGalleryContext} from './context'; | ||
import {useCallback, useRef} from '../../../src/preact'; | ||
|
||
/** | ||
* @param {!LightboxGalleryDef.Props} props | ||
* @return {PreactDef.Renderable} | ||
*/ | ||
export function LightboxGalleryProvider({children, render}) { | ||
const lightboxRef = useRef(null); | ||
const renderers = useRef([]); | ||
const lightboxElements = useRef([]); | ||
const register = (key, render) => { | ||
renderers.current[key] = render; | ||
}; | ||
const deregister = (key) => { | ||
delete lightboxElements.current[key]; | ||
delete renderers.current[key]; | ||
}; | ||
const context = { | ||
deregister, | ||
register, | ||
open: () => lightboxRef.current.open(), | ||
}; | ||
|
||
const renderElements = useCallback(() => { | ||
renderers.current.forEach((render, index) => { | ||
if (!lightboxElements.current[index]) { | ||
lightboxElements.current[index] = render(); | ||
} | ||
}); | ||
}, []); | ||
return ( | ||
<> | ||
<Lightbox | ||
onBeforeOpen={() => renderElements()} | ||
ref={lightboxRef} | ||
scrollable | ||
> | ||
{/* TODO: This needs an actual close button UI */} | ||
<div | ||
aria-label="Close the lightbox" | ||
role="button" | ||
tabIndex="0" | ||
onClick={() => lightboxRef.current.close()} | ||
> | ||
Close lightbox | ||
</div> | ||
<div>{lightboxElements.current}</div> | ||
</Lightbox> | ||
<LightboxGalleryContext.Provider value={context}> | ||
{render ? render() : children} | ||
</LightboxGalleryContext.Provider> | ||
</> | ||
); | ||
} |
Oops, something went wrong.