Skip to content

Commit

Permalink
feat: improve animations & Try using image component
Browse files Browse the repository at this point in the history
also refactor custom cms page into several components
  • Loading branch information
MathiasKandelborg committed Oct 29, 2020
1 parent e705fcd commit 7f8884c
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 138 deletions.
25 changes: 25 additions & 0 deletions components/HoC/Animation/CustomAnimatePresence.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import handleExitComplete from '@util/handleExitComplete'
import { AnimatePresence } from 'framer-motion'
import { FC } from 'react'

interface ICustomAnimatePresenceProps {
layoutShift: boolean
exitFirst?: boolean
}

const CustomAnimatePresence: FC<ICustomAnimatePresenceProps> = (props) => {
const { layoutShift, exitFirst, children } = props

return (
<>
<AnimatePresence
exitBeforeEnter={exitFirst}
onExitComplete={() => handleExitComplete()}
presenceAffectsLayout={layoutShift}>
{children}
</AnimatePresence>
</>
)
}

export default CustomAnimatePresence
8 changes: 4 additions & 4 deletions components/HoC/RenderPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import RenderSections from '@components/CMS/RenderSections'
import { PageAnimation, TitleAnimation } from '@components/UI'
import { APIRoute } from 'cms/APIRoute'
import { AnimatePresence } from 'framer-motion'
import CustomAnimatePresence from './Animation/CustomAnimatePresence'

const RenderPage: React.FC<{
loading: boolean
Expand All @@ -28,14 +28,14 @@ const RenderPage: React.FC<{

return (
<>
<AnimatePresence>
<CustomAnimatePresence exitFirst layoutShift>
<TitleAnimation key="title">
<h1>{title}</h1>
</TitleAnimation>
<PageAnimation key="title">
<PageAnimation key="page">
{content && <RenderSections sections={content} />}
</PageAnimation>
</AnimatePresence>
</CustomAnimatePresence>
</>
)
}
Expand Down
6 changes: 4 additions & 2 deletions components/HoC/SEO/Page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { common, seo } from '@util/settings'
import { common, CONSTANTS, seo } from '@util/settings'
import { NextSeo } from 'next-seo'
import { OpenGraphImages, OpenGraphProfile, Twitter } from 'next-seo/lib/types'

Expand All @@ -19,14 +19,16 @@ interface IPageSEOProps {
}

const PageSEO: React.FC<IPageSEOProps> = (props) => {
const seoImgUrl = CONSTANTS.DEV ? 'http://localhost:3000' : seo.url

const {
title = seo.title,
description = seo.description,
defaultImageHeight = 1250,
defaultImageWidth = 2500,
images = [
{
url: `${seo.url}/images/TRUE-logo-social-large-blue.png`,
url: `${seoImgUrl}/images/TRUE-logo-social-large-blue.png`,
height: defaultImageHeight,
width: defaultImageWidth
}
Expand Down
8 changes: 3 additions & 5 deletions components/UI/Pages/About/AboutPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import CustomAnimatePresence from '@components/HoC/Animation/CustomAnimatePresence'
import TitleWithDivider from '@components/HoC/TitleWithDivider'
import { PageAnimation } from '@components/UI'
import * as MUI from '@material-ui/core'
import { AnimatePresence } from 'framer-motion'
import { PageProps } from 'PageProps'
import aboutPageStyles from './AboutPage.styles'

Expand All @@ -14,9 +14,7 @@ const AboutPage: React.FC<IAboutPageProps> = () => {

return (
<>
<AnimatePresence
presenceAffectsLayout
onExitComplete={() => console.info('Exit complete!@')}>
<CustomAnimatePresence layoutShift exitFirst>
<TitleWithDivider key="about-title" variant="h1" text="About" />
<PageAnimation key="about-page" layoutID="page">
<Grid component={MUI.Paper} className={classes.root}>
Expand Down Expand Up @@ -51,7 +49,7 @@ const AboutPage: React.FC<IAboutPageProps> = () => {
</Typography>
</Grid>
</PageAnimation>
</AnimatePresence>
</CustomAnimatePresence>
</>
)
}
Expand Down
13 changes: 5 additions & 8 deletions components/UI/Pages/Category/CategoriesListPage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import CustomAnimatePresence from '@components/HoC/Animation/CustomAnimatePresence'
import TitleWithDivider from '@components/HoC/TitleWithDivider'
import { PageAnimation } from '@components/UI'
import ListCategory from '@components/UI/Category/ListCategory'
import * as MUI from '@material-ui/core'
import handleExitComplete from '@util/handleExitComplete'
import { Category } from 'cms/Category'
import { AnimatePresence } from 'framer-motion'
import categoriesPageListStyles from './CategoriesListPage.styles'

interface ICategoriesPageProps {
Expand All @@ -17,12 +16,10 @@ const CategoriesListPage: React.FC<ICategoriesPageProps> = (props) => {
const classes = categoriesPageListStyles()

return (
<AnimatePresence
presenceAffectsLayout
onExitComplete={() => handleExitComplete()}>
<TitleWithDivider variant="h1" text="Categories" />
<CustomAnimatePresence layoutShift>
<TitleWithDivider key="title" variant="h1" text="Categories" />

<PageAnimation layoutID="page">
<PageAnimation key="page">
<MUI.Grid
container
justify="space-around"
Expand All @@ -31,7 +28,7 @@ const CategoriesListPage: React.FC<ICategoriesPageProps> = (props) => {
<ListCategory categories={categories} />
</MUI.Grid>
</PageAnimation>
</AnimatePresence>
</CustomAnimatePresence>
)
}

Expand Down
13 changes: 5 additions & 8 deletions components/UI/Pages/Category/SingleCategoryPage.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import CustomAnimatePresence from '@components/HoC/Animation/CustomAnimatePresence'
import TitleWithDivider from '@components/HoC/TitleWithDivider'
import { PageAnimation } from '@components/UI'
import ListProduct from '@components/UI/Product/ListProduct'
import * as MUI from '@material-ui/core'
import handleExitComplete from '@util/handleExitComplete'
import { Category } from 'cms/Category'
import { Product } from 'cms/Product'
import { AnimatePresence } from 'framer-motion'
import categoriesPageListStyles from './CategoriesListPage.styles'

interface ICategoryPageProps {
Expand All @@ -19,11 +18,9 @@ const SingleCategoryPage: React.FC<ICategoryPageProps> = (props) => {
const classes = categoriesPageListStyles()

return (
<AnimatePresence
presenceAffectsLayout
onExitComplete={() => handleExitComplete()}>
<TitleWithDivider variant="h1" text={category?.title} />
<PageAnimation layoutID="page">
<CustomAnimatePresence layoutShift>
<TitleWithDivider key="title" variant="h1" text={category?.title} />
<PageAnimation key="page">
<MUI.Grid
container
alignItems="center"
Expand All @@ -36,7 +33,7 @@ const SingleCategoryPage: React.FC<ICategoryPageProps> = (props) => {
/>
</MUI.Grid>
</PageAnimation>
</AnimatePresence>
</CustomAnimatePresence>
)
}

Expand Down
4 changes: 2 additions & 2 deletions components/UI/Pages/Home/HomePage.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const homePageStyles = MUI.makeStyles((theme: MUI.Theme) =>
maxHeight: 24
},
headerImg: {
width: '100%',
height: '100%',
width: '960px',
height: '480px',
borderTopLeftRadius: 4,
borderTopRightRadius: 4
}
Expand Down
24 changes: 10 additions & 14 deletions components/UI/Pages/Home/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import CustomAnimatePresence from '@components/HoC/Animation/CustomAnimatePresence'
import TitleWithDivider from '@components/HoC/TitleWithDivider'
import { PageAnimation } from '@components/UI'
import * as MUI from '@material-ui/core'
// import handleExitComplete from '@util/handleExitComplete'
import { AnimatePresence } from 'framer-motion'
import { PageProps } from 'PageProps'
import Image from 'next/image'
import homePageStyles from './HomePage.styles'
import SectionBasedOnPracticesIdeas from './Section/BasedOnPracticesIdeas'
import SectionBasedOnThisTech from './Section/BasedOnThisTech'
Expand All @@ -20,22 +20,18 @@ const HomePage: React.FC<IHomePageProps> = (props) => {

return (
<>
<AnimatePresence
// exitBeforeEnter
// presenceAffectsLayout
// onExitComplete={() => handleExitComplete()}>
>
<CustomAnimatePresence exitFirst layoutShift>
<TitleWithDivider key="home-title" variant="h1" text={config.title} />

<PageAnimation key="home-page">
<Grid component={MUI.Paper} className={classes.root}>
<img
<Image
className={classes.headerImg}
width="960"
height="480"
srcSet="/images/TRUE-logo/TRUE-logo-social-small-blue.png 1x,
/images/TRUE-logo/TRUE-logo-social-large-blue.png 2x"
src="/images/TRUE-logo/TRUE-logo-social-large-small-blue.png"
sizes="30vw"
width={960}
height={480}
// /images/TRUE-logo/TRUE-logo-social-large-blue.png 2x"
src="/images/TRUE-logo/TRUE-logo-social-large-blue.png"
alt="TRUE Framework Logo Header"
/>
<h1>{translation![0].title}</h1>
Expand All @@ -52,7 +48,7 @@ const HomePage: React.FC<IHomePageProps> = (props) => {
<SectionBasedOnThisTech />
</Grid>
</PageAnimation>
</AnimatePresence>
</CustomAnimatePresence>
</>
)
}
Expand Down
9 changes: 5 additions & 4 deletions components/UI/Pages/Product/ProductPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SimpleBlockContent from '@components/CMS/PortableText/SimpleBlockContent'
import CustomAnimatePresence from '@components/HoC/Animation/CustomAnimatePresence'
import TitleWithDivider from '@components/HoC/TitleWithDivider'
import { PageAnimation } from '@components/UI'
import * as MUI from '@material-ui/core'
Expand All @@ -15,15 +16,15 @@ const SingleProductPage: React.FC<ISingleProductPageProps> = (props) => {
const classes = singleProductPageStyles()

return (
<>
<TitleWithDivider variant="h1" text={product?.title} />
<CustomAnimatePresence layoutShift>
<TitleWithDivider key="title" variant="h1" text={product?.title} />

<PageAnimation layoutID="page">
<PageAnimation key="page">
<MUI.Grid container component={MUI.Paper} className={classes.paper}>
<SimpleBlockContent blocks={product?.description} />
</MUI.Grid>
</PageAnimation>
</>
</CustomAnimatePresence>
)
}

Expand Down
28 changes: 28 additions & 0 deletions components/UI/Pages/page/DefaultPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import RenderPage from '@components/HoC/RenderPage'
import PageSEO from '@components/HoC/SEO/Page'
import { APIRoute } from 'cms/APIRoute'
import { FC } from 'react'

interface ICustomPageDefaultProps {
currentRoute: APIRoute
}

const CustomPageDefault: FC<ICustomPageDefaultProps> = (props) => {
const { currentRoute } = props

return (
<>
<PageSEO
title={currentRoute.page.title}
description={currentRoute.page.description}
/>
<RenderPage
preview={false}
loading={false}
pageProps={currentRoute.page}
/>
</>
)
}

export default CustomPageDefault
55 changes: 55 additions & 0 deletions components/UI/Pages/page/PreviewPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import RenderPage from '@components/HoC/RenderPage'
import PageSEO from '@components/HoC/SEO/Page'
import { MutationEvent } from '@sanity/client'
import { getClient } from '@util/api'
import { APIRoute } from 'cms/APIRoute'
import groq from 'groq'
import { useRouter } from 'next/router'
import { FC, useEffect, useState } from 'react'

interface ICustomPagePreviewProps {
currentRoute: APIRoute
}

const CustomPagePreview: FC<ICustomPagePreviewProps> = (props) => {
const { currentRoute } = props

const router = useRouter()
const [pageData, setRouteData] = useState(currentRoute)

const routeId = currentRoute?._id

useEffect(() => {
const sub = getClient(true)
.listen(groq`*[_type == "route" && _id == $routeId][0]`, {
routeId
})
.subscribe(
(update: MutationEvent<APIRoute>) => {
const pageRes = update.result

if (pageRes) setRouteData(pageRes)
},
(err) => console.error(err),
() => {}
)

return () => sub.unsubscribe()
}, [routeId])

return (
<>
<PageSEO
title={pageData?.page?.title || 'loading'}
description={pageData?.page?.description || 'loading'}
/>
<RenderPage
preview
loading={router.isFallback}
pageProps={pageData?.page}
/>
</>
)
}

export default CustomPagePreview
3 changes: 3 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const conf = {
dest: 'public',
runtimeCaching
},
images: {
deviceSizes: [600, 960, 1280, 1920]
},
i18n: {
locales: ['en-US', 'da-DK', 'da', 'en'],
defaultLocale: 'en-US',
Expand Down
Loading

0 comments on commit 7f8884c

Please sign in to comment.