Skip to content

Commit

Permalink
refactor(scrollContainer): rename to object with methods simply
Browse files Browse the repository at this point in the history
  • Loading branch information
manudeli committed Feb 9, 2023
1 parent 5fa9ea2 commit 6ba5761
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 42 deletions.
4 changes: 2 additions & 2 deletions apps/web/components/domains/ReservationItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Props {
}

const ReservationItem = ({ reservation }: Props) => {
const { scrollContainerWidth } = useScrollContainer()
const scrollContainer = useScrollContainer()
const getFavoritesQuery = useGetFavoritesQuery()
const deleteReservationMutation = useDeleteReservationMutation()
const queryClient = useQueryClient()
Expand Down Expand Up @@ -87,7 +87,7 @@ const ReservationItem = ({ reservation }: Props) => {
/>
<Box
width="90%"
maxWidth={`${scrollContainerWidth - 60}px`}
maxWidth={`${scrollContainer.width - 60}px`}
bgColor="white"
borderRadius="16px"
p="16px"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const Cell = ({ timeNumber, date, onClick }: Props) => {
isNeedToScrollToThisCell
) {
const rect = ref.current.getBoundingClientRect()
scrollContainer.scrollTo(rect.bottom + tableCellHeight * 3)
scrollContainer.to(rect.bottom + tableCellHeight * 3)
}
}, [])

Expand Down
10 changes: 5 additions & 5 deletions apps/web/layouts/BottomFixedGradient/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const BottomFixedGradient = ({
children,
}: ComponentPropsWithoutRef<typeof Flex>) => {
const theme = useTheme()
const { scrollContainerWidth, scrollContainerRef } = useScrollContainer()
const scrollContainer = useScrollContainer()

const [portal, setPortal] = useState<ReactPortal | null>(null)

Expand All @@ -23,7 +23,7 @@ const BottomFixedGradient = ({
as={motion.div}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
w={`${scrollContainerWidth}px`}
w={`${scrollContainer.width}px`}
h="120px"
pos="fixed"
bottom={0}
Expand All @@ -39,7 +39,7 @@ const BottomFixedGradient = ({
as={motion.div}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
w={`${scrollContainerWidth}px`}
w={`${scrollContainer.width}px`}
pos="fixed"
bottom={0}
maxW="640px"
Expand All @@ -48,10 +48,10 @@ const BottomFixedGradient = ({
{children}
</Box>
</>,
scrollContainerRef.current!
scrollContainer.ref.current!
)
)
}, [children, scrollContainerWidth, scrollContainerRef, theme.colors.white])
}, [children, scrollContainer.width, scrollContainer.ref, theme.colors.white])

return portal
}
Expand Down
4 changes: 2 additions & 2 deletions apps/web/layouts/Layout/components/PageLoader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const PageLoader = () => {
})
const [isProgressBar, setIsProgressBar] = useState(false)

const { scrollContainerWidth } = useScrollContainer()
const scrollContainer = useScrollContainer()
const setNavigation = useSetNavigation()

useEffect(() => {
Expand Down Expand Up @@ -55,7 +55,7 @@ const PageLoader = () => {
<div
css={css`
position: fixed;
width: ${scrollContainerWidth}px;
width: ${scrollContainer.width}px;
height: 100%;
`}
>
Expand Down
29 changes: 11 additions & 18 deletions apps/web/layouts/Layout/components/ScrollContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { createContext, useContext, useRef } from "react"
import { css, useTheme } from "@emotion/react"

type Value = {
scrollTo: (top: number) => void
scrollToTop: () => void
scrollContainerRef: RefObject<HTMLDivElement>
scrollContainerHeight: number
scrollContainerWidth: number
to: (top: number) => void
toTop: () => void
ref: RefObject<HTMLDivElement>
height: number
width: number
}

const Context = createContext({} as Value)
Expand All @@ -20,24 +20,17 @@ type Props = {

const ScrollContainer = ({ children }: Props) => {
const ref = useRef<HTMLDivElement>(null)

const theme = useTheme()

const scrollTo = (top: number) =>
ref.current?.scrollTo({ top, behavior: "smooth" })
const scrollToTop = () => scrollTo(0)

const scrollContainerHeight = ref.current?.getClientRects()[0].height ?? 0
const scrollContainerWidth = ref.current?.getClientRects()[0].width ?? 0
const to = (top: number) => ref.current?.scrollTo({ top, behavior: "smooth" })

return (
<Context.Provider
value={{
scrollTo,
scrollToTop,
scrollContainerRef: ref,
scrollContainerHeight,
scrollContainerWidth,
to,
toTop: () => to(0),
ref,
height: ref.current?.getClientRects()[0].height ?? 0,
width: ref.current?.getClientRects()[0].width ?? 0,
}}
>
<div
Expand Down
4 changes: 2 additions & 2 deletions apps/web/layouts/Layout/navigations/TopNavigation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TopNavigation = ({ isShrink }: Props) => {
const router = useRouter()

const currentUserQuery = useCurrentUserQuery()
const { scrollToTop } = useScrollContainer()
const scrollContainer = useScrollContainer()
const navigation = useNavigationValue()

if (!navigation.top) {
Expand Down Expand Up @@ -127,7 +127,7 @@ const TopNavigation = ({ isShrink }: Props) => {

<AnimatePresence mode="wait" key={navigation.top.title}>
<motion.div
onClick={scrollToTop}
onClick={scrollContainer.toTop}
css={css`
display: flex;
align-items: center;
Expand Down
8 changes: 3 additions & 5 deletions apps/web/pages/courts/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ type FieldValues = Pick<
const Page = () => {
const router = useRouter()
const courtCreateMutation = useCourtCreateMutation()

const { scrollContainerHeight } = useScrollContainer()
const scrollContainer = useScrollContainer()

const {
control,
Expand Down Expand Up @@ -159,7 +158,7 @@ const Page = () => {
as={motion.div}
initial={{ padding: 16 }}
animate={
scrollContainerHeight > 400 ? { padding: 16 } : { padding: 0 }
scrollContainer.height > 400 ? { padding: 16 } : { padding: 0 }
}
>
<Button
Expand Down Expand Up @@ -193,7 +192,7 @@ const Page = () => {
)}
fullWidth
size="lg"
style={{ borderRadius: scrollContainerHeight > 400 ? 16 : 0 }}
style={{ borderRadius: scrollContainer.height > 400 ? 16 : 0 }}
>
새 농구장 추가 제안하기
</Button>
Expand Down Expand Up @@ -267,7 +266,6 @@ const MapEditor = ({
height: isEdited ? 200 : 400,
borderRadius: 16,
zIndex: 0,
// transition: "height 1s ease 200ms",
}}
onBoundChange={(map) => {
map.relayout()
Expand Down
4 changes: 2 additions & 2 deletions apps/web/pages/reservations/courts/[courtId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const Contents = withSuspense.CSROnly(Suspended, { fallback: undefined })
function Suspended({ courtId, date }: Props) {
const theme = useTheme()
const router = useRouter()
const { scrollContainerWidth } = useScrollContainer()
const scrollContainer = useScrollContainer()
const queryClient = useQueryClient()

const createReservationMutation = useCreateReservationMutation(courtId)
Expand Down Expand Up @@ -265,7 +265,7 @@ function Suspended({ courtId, date }: Props) {
/>
<Box
width="90%"
maxWidth={`${scrollContainerWidth - 60}px`}
maxWidth={`${scrollContainer.width - 60}px`}
bgColor="white"
borderRadius="16px"
p="16px"
Expand Down
6 changes: 3 additions & 3 deletions apps/web/pages/user/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const Contents = () => {
}

const EditForm = ({ initialData }: { initialData: APIUser }) => {
const { scrollContainerHeight } = useScrollContainer()
const scrollContainer = useScrollContainer()
const router = useRouter()
const myProfileMutation = useMyProfileMutation()

Expand Down Expand Up @@ -239,7 +239,7 @@ const EditForm = ({ initialData }: { initialData: APIUser }) => {
as={motion.div}
initial={{ padding: 16 }}
animate={
scrollContainerHeight > 400 ? { padding: 16 } : { padding: 0 }
scrollContainer.height > 400 ? { padding: 16 } : { padding: 0 }
}
>
<Button
Expand All @@ -248,7 +248,7 @@ const EditForm = ({ initialData }: { initialData: APIUser }) => {
loading={isSubmitting || isSubmitSuccessful}
disabled={!isDirty || !isValid || isSubmitSuccessful}
onClick={handleSubmit(onSubmit)}
style={{ borderRadius: scrollContainerHeight > 400 ? 16 : 0 }}
style={{ borderRadius: scrollContainer.height > 400 ? 16 : 0 }}
>
{!isDirty || !isValid
? "프로필을 먼저 수정하세요"
Expand Down
4 changes: 2 additions & 2 deletions apps/web/pages/user/menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Page: NextPage<InferGetStaticPropsType<typeof getStaticProps>> = ({
buildTime,
}) => {
const router = useRouter()
const { scrollContainerWidth } = useScrollContainer()
const scrollContainer = useScrollContainer()
const queryClient = useQueryClient()

return (
Expand Down Expand Up @@ -62,7 +62,7 @@ const Page: NextPage<InferGetStaticPropsType<typeof getStaticProps>> = ({
/>
<Box
width="90%"
maxWidth={`${scrollContainerWidth - 60}px`}
maxWidth={`${scrollContainer.width - 60}px`}
bgColor="white"
borderRadius="16px"
p="16px"
Expand Down

0 comments on commit 6ba5761

Please sign in to comment.