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

#387 add theme prop support to viewer component #390

Merged
merged 5 commits into from
Jan 21, 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
22 changes: 20 additions & 2 deletions packages/miew-react/src/Viewer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { render } from '@testing-library/react'
import 'jest-canvas-mock'
import { Viewer } from 'src/Viewer'
import { Viewer } from './Viewer'

// TODO create mock folder, and move all mocks there
jest.mock('use-resize-observer', () =>
jest.fn().mockReturnValue({ ref: null, width: 100, height: 100 })
)
Expand All @@ -11,8 +10,27 @@ jest.mock('miew', () => ({
Miew: jest.fn(() => ({ init: jest.fn(() => true), run: jest.fn() }))
}))

const customTheme = {
palette: {
accent: {
main: '#f00'
},
primary: {
main: '#e2b6b6',
light: '#fcf8f8',
dark: '#8e4343'
}
},
typography: {
fontSize: 14
}
}

describe('Viewer component', () => {
it('should be rendered', () => {
expect(render(<Viewer />)).toMatchSnapshot()
})
it('should apply custom theme when theme prop is provided', () => {
expect(render(<Viewer theme={customTheme} />)).toMatchSnapshot()
})
})
40 changes: 30 additions & 10 deletions packages/miew-react/src/Viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
import { useLayoutEffect, useRef } from 'react'
import useResizeObserver from 'use-resize-observer'
import { Provider } from 'react-redux'
import { Miew, MiewOptions } from 'miew'
import { Theme } from '@emotion/react'
import useResizeObserver from 'use-resize-observer'
import { Theme, ThemeProvider } from '@emotion/react'
import { createTheme } from '@mui/material/styles'
import { CssBaseline } from '@mui/material'
import { merge } from 'lodash'
import { store } from 'state'
import { defaultTheme, MiewTheme } from './theming'

const MEDIA_SIZES = {
smallWidth: 800,
smallHeight: 400
}

type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>
}

type ViewerProps = {
onInit?: (miew: object) => void
onInit?: (miew: Miew) => void
options?: MiewOptions
theme?: DeepPartial<MiewTheme>
}

const Viewer = (props: ViewerProps) => {
const { onInit, options } = props
const muiTheme = createTheme()

const Viewer = ({ onInit, options, theme }: ViewerProps) => {
const viewerTheme = theme ? merge(defaultTheme, theme) : defaultTheme

const ref = useRef<HTMLDivElement>(null)
const { width, height } = useResizeObserver<HTMLDivElement>({ ref })

const isSizeSmall =
(height && height <= MEDIA_SIZES.smallHeight) ||
(width && width <= MEDIA_SIZES.smallWidth)

const viewerStyle = (theme: Theme) => {
const palette = theme?.customTheme?.palette
const palette = theme.miew.palette
return {
backgroundColor: isSizeSmall
? palette?.accent?.main
: palette?.primary?.main,
backgroundColor: isSizeSmall ? palette.accent.main : palette.primary.main,
height: '100%',
width: '100%',
'& > .miew-canvas': {
Expand All @@ -45,7 +58,14 @@ const Viewer = (props: ViewerProps) => {
if (typeof onInit === 'function') onInit(miew)
}, [options, onInit])

return <div ref={ref} css={viewerStyle} />
return (
<Provider store={store}>
<ThemeProvider theme={merge(muiTheme, { miew: viewerTheme })}>
<CssBaseline />
<div ref={ref} css={viewerStyle} />
</ThemeProvider>
</Provider>
)
}

export { Viewer }
21 changes: 0 additions & 21 deletions packages/miew-react/src/ViewerContainer.tsx

This file was deleted.

93 changes: 93 additions & 0 deletions packages/miew-react/src/__snapshots__/Viewer.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,9 +1,101 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Viewer component should apply custom theme when theme prop is provided 1`] = `
Object {
"asFragment": [Function],
"baseElement": .emotion-0 {
background-color: #f00;
height: 100%;
width: 100%;
}

.emotion-0>.miew-canvas {
height: 100%;
width: 100%;
}

<body>
<div>
<div
class="emotion-0"
/>
</div>
</body>,
"container": .emotion-0 {
background-color: #f00;
height: 100%;
width: 100%;
}

.emotion-0>.miew-canvas {
height: 100%;
width: 100%;
}

<div>
<div
class="emotion-0"
/>
</div>,
"debug": [Function],
"findAllByAltText": [Function],
"findAllByDisplayValue": [Function],
"findAllByLabelText": [Function],
"findAllByPlaceholderText": [Function],
"findAllByRole": [Function],
"findAllByTestId": [Function],
"findAllByText": [Function],
"findAllByTitle": [Function],
"findByAltText": [Function],
"findByDisplayValue": [Function],
"findByLabelText": [Function],
"findByPlaceholderText": [Function],
"findByRole": [Function],
"findByTestId": [Function],
"findByText": [Function],
"findByTitle": [Function],
"getAllByAltText": [Function],
"getAllByDisplayValue": [Function],
"getAllByLabelText": [Function],
"getAllByPlaceholderText": [Function],
"getAllByRole": [Function],
"getAllByTestId": [Function],
"getAllByText": [Function],
"getAllByTitle": [Function],
"getByAltText": [Function],
"getByDisplayValue": [Function],
"getByLabelText": [Function],
"getByPlaceholderText": [Function],
"getByRole": [Function],
"getByTestId": [Function],
"getByText": [Function],
"getByTitle": [Function],
"queryAllByAltText": [Function],
"queryAllByDisplayValue": [Function],
"queryAllByLabelText": [Function],
"queryAllByPlaceholderText": [Function],
"queryAllByRole": [Function],
"queryAllByTestId": [Function],
"queryAllByText": [Function],
"queryAllByTitle": [Function],
"queryByAltText": [Function],
"queryByDisplayValue": [Function],
"queryByLabelText": [Function],
"queryByPlaceholderText": [Function],
"queryByRole": [Function],
"queryByTestId": [Function],
"queryByText": [Function],
"queryByTitle": [Function],
"rerender": [Function],
"unmount": [Function],
}
`;

exports[`Viewer component should be rendered 1`] = `
Object {
"asFragment": [Function],
"baseElement": .emotion-0 {
background-color: #2E6DA4;
height: 100%;
width: 100%;
}
Expand All @@ -21,6 +113,7 @@ Object {
</div>
</body>,
"container": .emotion-0 {
background-color: #2E6DA4;
height: 100%;
width: 100%;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/miew-react/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { ViewerContainer } from './ViewerContainer'
import { Viewer } from './Viewer'

export default ViewerContainer
export default Viewer
25 changes: 25 additions & 0 deletions packages/miew-react/src/theming/defaultTheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { MiewTheme } from './theme.types'

export const defaultTheme: MiewTheme = {
palette: {
accent: {
main: '#2E6DA4',
dark: '#204D74',
light: '#337AB7'
},
primary: {
main: '#DDD',
light: '#FFF',
dark: '#DDD'
},
secondary: {
main: '#333',
light: '#C0C0C0',
dark: '#383838'
}
},
typography: {
fontSize: 16,
fontFamily: 'sans-serif'
}
}
3 changes: 2 additions & 1 deletion packages/miew-react/src/theming/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './mainTheme'
export * from './defaultTheme'
export * from './theme.types'
18 changes: 0 additions & 18 deletions packages/miew-react/src/theming/mainTheme.ts

This file was deleted.

17 changes: 17 additions & 0 deletions packages/miew-react/src/theming/theme.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type ColorSet = {
main: string
light: string
dark: string
}

export interface MiewTheme {
palette: {
accent: ColorSet
primary: ColorSet
secondary: ColorSet
}
typography: {
fontSize: number
fontFamily: string
}
}
26 changes: 4 additions & 22 deletions packages/miew-react/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Theme as MuiTheme } from '@mui/material'
import { MiewTheme } from 'theming'

declare module '*.svg' {
import * as React from 'react'
Expand All @@ -11,26 +11,8 @@ declare module '*.svg' {
export default src
}

interface CustomTheme {
customTheme?: {
palette?: {
accent?: {
main?: string
dark?: string
}
primary?: {
main?: string
light?: string
dark?: string
}
}
typography?: {
htmlFontSize?: number
}
}
}

declare module '@emotion/react' {
/* eslint-disable @typescript-eslint/no-empty-interface */
export interface Theme extends MuiTheme, CustomTheme {}
export interface Theme {
miew: MiewTheme
}
}
3 changes: 2 additions & 1 deletion packages/miew-react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"paths": {
"components/*": ["src/components/*"],
"state/*": ["src/state/*"],
"state": ["src/state"]
"state": ["src/state"],
"theming": ["src/theming"]
},
},
"include": [ "src" ],
Expand Down
Loading