Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(page): add props to control the style of dot #687

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions components/page/__tests__/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,36 @@ exports[`Page should render correctly 1`] = `
}
</style></section>"
`;

exports[`Page should work correctly with dot configs 1`] = `
"<section><main class=\\"\\"><style>
main {
font-size: calc(1 * 16px);
width: 100%;
height: 100%;
padding: calc(3.125 * 16px) 0 calc(3.125 * 16px) 0;
margin: 0 0 0 0;
}
</style></main><span><style>
:global(body) {
background-image: radial-gradient(#e3e3e3 20px, transparent 0),
radial-gradient(#e3e3e3 20px, transparent 0);
background-position: 0 0, calc(0.5 * 25px) calc(0.5 * 25px);
background-attachment: fixed;
background-size: calc(0.5 * 50px) calc(0.5 * 50px);
}
</style></span><style>
section {
max-width: 100vw;
min-height: 100vh;
box-sizing: border-box;
position: relative;
font-size: calc(1 * 16px);
width: calc(100% - 100pt);
height: auto;
padding: 0 calc(1.34 * 16px) 0 calc(1.34 * 16px);
margin: 0 auto 0
auto;
}
</style></section>"
`;
6 changes: 6 additions & 0 deletions components/page/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ describe('Page', () => {
)
expect(wrapper.html()).not.toContain('global(body)')
})

it('should work correctly with dot configs', () => {
const wrapper = mount(<Page dotBackdrop dotSize="20px" dotSpace={0.5} />)
expect(wrapper.html()).toMatchSnapshot()
expect(() => wrapper.unmount()).not.toThrow()
})
})
50 changes: 35 additions & 15 deletions components/page/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo, useState } from 'react'
import React, { CSSProperties, useEffect, useMemo, useState } from 'react'
import { tuple } from '../utils/prop-types'
import useTheme from '../use-theme'
import PageContent from './page-content'
Expand All @@ -12,26 +12,44 @@ export type PageRenderMode = typeof renderMode[number]
interface Props {
render?: PageRenderMode
dotBackdrop?: boolean
dotSize?: CSSProperties['fontSize']
dotSpace?: number
}

const defaultProps = {
render: 'default' as PageRenderMode,
dotBackdrop: false,
dotSize: '1px' as CSSProperties['fontSize'],
dotSpace: 1,
}

const DotStyles: React.FC<unknown> = () => (
<span>
<style jsx>{`
:global(body) {
background-image: radial-gradient(#e3e3e3 1px, transparent 0),
radial-gradient(#e3e3e3 1px, transparent 0);
background-position: 0 0, 25px 25px;
background-attachment: fixed;
background-size: 50px 50px;
}
`}</style>
</span>
)
export type DotStylesProps = {
dotSize: CSSProperties['fontSize']
dotSpace: number
}

const DotStyles: React.FC<DotStylesProps> = ({ dotSpace, dotSize }) => {
const background = useMemo(
() => ({
position: `calc(${dotSpace} * 25px)`,
size: `calc(${dotSpace} * 50px)`,
}),
[dotSpace],
)
return (
<span>
<style jsx>{`
:global(body) {
background-image: radial-gradient(#e3e3e3 ${dotSize}, transparent 0),
radial-gradient(#e3e3e3 ${dotSize}, transparent 0);
background-position: 0 0, ${background.position} ${background.position};
background-attachment: fixed;
background-size: ${background.size} ${background.size};
}
`}</style>
</span>
)
}

type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
export type PageProps = Props & NativeAttrs
Expand All @@ -41,6 +59,8 @@ const PageComponent: React.FC<React.PropsWithChildren<PageProps>> = ({
render,
dotBackdrop,
className,
dotSize,
dotSpace,
...props
}: React.PropsWithChildren<PageProps> & typeof defaultProps) => {
const theme = useTheme()
Expand Down Expand Up @@ -76,7 +96,7 @@ const PageComponent: React.FC<React.PropsWithChildren<PageProps>> = ({
return (
<section className={className} {...withPureProps(props)}>
{hasContent ? children : <PageContent>{children}</PageContent>}
{showDot && <DotStyles />}
{showDot && <DotStyles dotSize={dotSize} dotSpace={dotSpace} />}
<style jsx>{`
section {
max-width: 100vw;
Expand Down
4 changes: 3 additions & 1 deletion pages/en-us/components/page.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Layout, Playground, ExampleBlock, Attributes } from 'lib/components'
import { Layout, Playground, Attributes } from 'lib/components'
import { Page, Button } from 'components'
import MockPage from 'lib/components/displays/mock-page'

Expand Down Expand Up @@ -72,6 +72,8 @@ Container of general for display page content.
| --------------- | ---------------------------------- | --------------------------------- | ------------------------ | --------- |
| **render** | render mode | [PageRenderMode](#pagerendermode) | - | `default` |
| **dotBackdrop** | show decorations in the background | `boolean` | - | `false` |
| **dotSize** | dot size in the background | `CSSProperties` | - | `1px` |
| **dotSpace** | space multiple between each dot | `number` | - | `1` |
| ... | native props | `HTMLAttributes` | `'id', 'className', ...` | - |

<Attributes.Title>Page.Header.Props</Attributes.Title>
Expand Down
4 changes: 3 additions & 1 deletion pages/zh-cn/components/page.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Layout, Playground, ExampleBlock, Attributes } from 'lib/components'
import { Layout, Playground, Attributes } from 'lib/components'
import { Page, Button } from 'components'
import MockPage from 'lib/components/displays/mock-page'

Expand Down Expand Up @@ -72,6 +72,8 @@ export const meta = {
| --------------- | -------------------- | --------------------------------- | --------------------------------------- | --------- |
| **render** | 渲染方式 | [PageRenderMode](#pagerendermode) | - | `default` |
| **dotBackdrop** | 在背景中显示装饰样式 | `boolean` | - | `false` |
| **dotSize** | 背景中的点大小 | `CSSProperties` | - | `1px` |
| **dotSpace** | 背景中每个点的间距 | `number` | - | `1` |
| ... | 原生属性 | `HTMLAttributes` | `'autoFocus', 'name', 'className', ...` | - |

<Attributes.Title>Page.Header.Props</Attributes.Title>
Expand Down