-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contributions): ajout du support des infographies dans les contr…
…ibutions
- Loading branch information
Showing
5 changed files
with
139 additions
and
2 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
44 changes: 44 additions & 0 deletions
44
packages/code-du-travail-frontend/src/modules/common/ImageWrapper.tsx
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,44 @@ | ||
import React, { useState } from "react"; | ||
import Lightbox from "react-image-lightbox"; | ||
|
||
import { css } from "@styled-system/css"; | ||
import useScrollBlock from "../utils/useScrollBlock"; | ||
|
||
type Props = { | ||
altText: string; | ||
src: string; | ||
}; | ||
|
||
const InfographicWrapper = ({ altText, src }: Props): JSX.Element => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [blockScroll, allowScroll] = useScrollBlock(); | ||
|
||
const onOpen = () => { | ||
setIsOpen(true); | ||
blockScroll(); | ||
}; | ||
|
||
const onClose = () => { | ||
setIsOpen(false); | ||
allowScroll(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<img | ||
src={src} | ||
alt={altText} | ||
onClick={onOpen} | ||
aria-hidden="true" | ||
className={`${ImageZoom}`} | ||
/> | ||
{isOpen && <Lightbox mainSrc={src} onCloseRequest={onClose} />} | ||
</> | ||
); | ||
}; | ||
|
||
const ImageZoom = css({ | ||
cursor: "zoom-in", | ||
}); | ||
|
||
export default InfographicWrapper; |
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
54 changes: 54 additions & 0 deletions
54
packages/code-du-travail-frontend/src/modules/utils/useScrollBlock.ts
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,54 @@ | ||
import { useRef } from "react"; | ||
|
||
const safeDocument: any = typeof document !== "undefined" ? document : {}; | ||
|
||
/** | ||
* Usage: | ||
* const [blockScroll, allowScroll] = useScrollBlock(); | ||
*/ | ||
const useScrollBlock = (): [() => void, () => void] => { | ||
const scrollBlocked = useRef(false); | ||
const html = safeDocument.documentElement; | ||
const { body } = safeDocument; | ||
|
||
const blockScroll = (): void => { | ||
if (!body || !body.style || scrollBlocked.current) return; | ||
if (document == undefined) return; | ||
|
||
const scrollBarWidth = window.innerWidth - html.clientWidth; | ||
const bodyPaddingRight = | ||
parseInt( | ||
window.getComputedStyle(body).getPropertyValue("padding-right") | ||
) || 0; | ||
|
||
/** | ||
* 1. Fixes a bug in iOS and desktop Safari whereby setting | ||
* `overflow: hidden` on the html/body does not prevent scrolling. | ||
* 2. Fixes a bug in desktop Safari where `overflowY` does not prevent | ||
* scroll if an `overflow-x` style is also applied to the body. | ||
*/ | ||
html.style.position = "relative"; /* [1] */ | ||
html.style.overflow = "hidden"; /* [2] */ | ||
body.style.position = "relative"; /* [1] */ | ||
body.style.overflow = "hidden"; /* [2] */ | ||
body.style.paddingRight = `${bodyPaddingRight + scrollBarWidth}px`; | ||
|
||
scrollBlocked.current = true; | ||
}; | ||
|
||
const allowScroll = (): void => { | ||
if (!body || !body.style || !scrollBlocked.current) return; | ||
|
||
html.style.position = ""; | ||
html.style.overflow = ""; | ||
body.style.position = ""; | ||
body.style.overflow = ""; | ||
body.style.paddingRight = ""; | ||
|
||
scrollBlocked.current = false; | ||
}; | ||
|
||
return [blockScroll, allowScroll]; | ||
}; | ||
|
||
export default useScrollBlock; |