Skip to content

Commit

Permalink
Ajout d'un scrollToTop sur l'onLoad de l'iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
wiinxt committed Mar 14, 2022
1 parent 4b14a27 commit 56eb238
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
40 changes: 26 additions & 14 deletions site/source/components/PlaceDesEntreprises.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { lazy, Suspense, useEffect } from 'react'
import { lazy, ReactEventHandler, Suspense, useEffect, useRef } from 'react'
import styled from 'styled-components'
import Emoji from './utils/Emoji'
import { iframeResize } from 'iframe-resizer'
Expand All @@ -19,7 +19,13 @@ const IframeContainer = styled.div`
margin: 0 -3rem;
`

export const PlacesDesEntreprisesIframe = ({ src }: { src: string }) => {
export const PlacesDesEntreprisesIframe = ({
src,
onLoad,
}: {
src: string
onLoad?: ReactEventHandler<HTMLIFrameElement>
}) => {
useEffect(() => {
iframeResize({}, '#pdeIframe')
}, [])
Expand All @@ -31,6 +37,7 @@ export const PlacesDesEntreprisesIframe = ({ src }: { src: string }) => {
src={src}
frameBorder="0"
id="pdeIframe"
onLoad={onLoad}
/>
</IframeContainer>
)
Expand Down Expand Up @@ -68,6 +75,12 @@ export const PlacesDesEntreprisesButton = ({
: 'reso-staging.osc-fr1.scalingo.io')
const url = new URL(baseURL + pathname)

const contentRef = useRef<HTMLDivElement>(null)

const scrollTo = (x: number, y: number) => {
contentRef.current?.scrollTo(x, y)
}

if (siret) {
url.searchParams.set('siret', siret)
}
Expand All @@ -82,7 +95,17 @@ export const PlacesDesEntreprisesButton = ({
<ButtonLabel>{t('Échanger avec un conseiller')}</ButtonLabel>
</Button>
)}
contentRef={contentRef}
>
<Body>
<Trans>
Décrivez votre projet ou votre problème en donnant quelques éléments
de contexte. Nous identifions, parmi l’ensemble des partenaires
publics et parapublics, le conseiller compétent pour votre demande.
Celui-ci vous contacte par téléphone sous 5 jours et vous accompagne
en fonction de votre situation.
</Trans>
</Body>
<Suspense
fallback={
<Container
Expand All @@ -95,18 +118,7 @@ export const PlacesDesEntreprisesButton = ({
</Container>
}
>
<>
<Body>
<Trans>
Décrivez votre projet ou votre problème en donnant quelques
éléments de contexte. Nous identifions, parmi l’ensemble des
partenaires publics et parapublics, le conseiller compétent pour
votre demande. Celui-ci vous contacte par téléphone sous 5 jours
et vous accompagne en fonction de votre situation.
</Trans>
</Body>
<LazyIframe src={url.href} />
</>
<LazyIframe src={url.href} onLoad={() => scrollTo(0, 0)} />
</Suspense>
</PopoverWithTrigger>
</Container>
Expand Down
7 changes: 4 additions & 3 deletions site/source/design-system/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
usePreventScroll,
} from '@react-aria/overlays'
import { AriaDialogProps } from '@react-types/dialog'
import React, { useEffect, useRef, useState } from 'react'
import React, { RefObject, useEffect, useRef, useState } from 'react'
import styled, { css, keyframes, ThemeProvider } from 'styled-components'
import { Container } from './layout'
import { H2 } from './typography/heading'
Expand Down Expand Up @@ -44,9 +44,10 @@ export default function Popover(
children: React.ReactNode
title?: string
small?: boolean
contentRef?: RefObject<HTMLDivElement>
}
) {
const { title, children, small } = props
const { title, children, small, contentRef } = props

// Handle interacting outside the dialog and pressing
// the Escape key to close the modal.
Expand Down Expand Up @@ -118,7 +119,7 @@ export default function Popover(
</CloseButton>
</CloseButtonContainer>
)}
<PopoverContent>
<PopoverContent ref={contentRef}>
{title && (
<H2 as="h1" {...titleProps}>
{title}
Expand Down
12 changes: 11 additions & 1 deletion site/source/design-system/PopoverWithTrigger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { useOverlayTrigger } from '@react-aria/overlays'
import { useOverlayTriggerState } from '@react-stately/overlays'
import { AriaButtonProps } from '@react-types/button'
import { Button } from '@/design-system/buttons'
import React, { ReactElement, Ref, useEffect, useMemo, useRef } from 'react'
import React, {
ReactElement,
Ref,
RefObject,
useEffect,
useMemo,
useRef,
} from 'react'
import { useLocation } from 'react-router'
import Popover from './Popover'
import { Link } from './typography/link'
Expand All @@ -18,13 +25,15 @@ type PopoverWithTriggerProps = {
children: React.ReactNode | ((close: () => void) => React.ReactNode)
title?: string
small?: boolean
contentRef?: RefObject<HTMLDivElement>
}

export default function PopoverWithTrigger({
children,
title,
trigger,
small,
contentRef,
}: PopoverWithTriggerProps) {
const state = useOverlayTriggerState({})
const openButtonRef = useRef<HTMLButtonElement>(null)
Expand Down Expand Up @@ -66,6 +75,7 @@ export default function PopoverWithTrigger({
isDismissable
role="dialog"
small={small}
contentRef={contentRef}
>
{typeof children === 'function'
? children(() => state.close())
Expand Down

0 comments on commit 56eb238

Please sign in to comment.