Skip to content

Commit

Permalink
refactor: use hooks in ObjectFitProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Sep 17, 2021
1 parent 87aad56 commit 8dea133
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 50 deletions.
4 changes: 2 additions & 2 deletions packages/griffith/src/components/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import {
Subscription,
} from '../contexts/MessageContext'
import VideoSourceContext from '../contexts/VideoSourceContext'
import ObjectFitContext from '../contexts/ObjectFitContext'
import ObjectFitContext, {ObjectFit} from '../contexts/ObjectFitContext'
import PositionProvider from '../contexts/PositionProvider'
import ObjectFitProvider, {ObjectFit} from '../contexts/ObjectFitProvider'
import ObjectFitProvider from '../contexts/ObjectFitProvider'
import LocaleProvider from '../contexts/LocaleProvider'
import Time from './Time'
import Icon from './Icon'
Expand Down
4 changes: 3 additions & 1 deletion packages/griffith/src/contexts/ObjectFitContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react'

type ObjectFit = 'fill' | 'contain' | 'cover' | 'none' | 'scale-down'
// https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
export type ObjectFit = 'fill' | 'contain' | 'cover' | 'none' | 'scale-down'

type ObjectFitContextValue = {
objectFit?: ObjectFit
setObjectFit?(objectFit: ObjectFit): void
Expand Down
73 changes: 26 additions & 47 deletions packages/griffith/src/contexts/ObjectFitProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,32 @@
import React from 'react'
import ObjectFitContext from './ObjectFitContext'

// https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
const VALID_FIT = ['fill', 'contain', 'cover', 'none', 'scale-down'] as const
export type ObjectFit = typeof VALID_FIT[number]
import React, {useEffect, useMemo, useState} from 'react'
import ObjectFitContext, {ObjectFit} from './ObjectFitContext'

type Props = {
initialObjectFit: ObjectFit
initialObjectFit?: ObjectFit
}

type State = {
objectFit: ObjectFit
const ObjectFitProvider: React.FC<Props> = ({
initialObjectFit = 'contain',
children,
}) => {
const [objectFit, setObjectFit] = useState<ObjectFit>(initialObjectFit)
const contextValue = useMemo(
() => ({
objectFit,
setObjectFit,
}),
[objectFit, setObjectFit]
)

useEffect(() => {
setObjectFit(initialObjectFit)
}, [initialObjectFit])

return (
<ObjectFitContext.Provider value={contextValue}>
{children}
</ObjectFitContext.Provider>
)
}

export default class ObjectFitProvider extends React.Component<Props, State> {
static defaultProps = {
initialObjectFit: 'contain',
}

state = {
objectFit: this.props.initialObjectFit,
}

setObjectFit = (objectFit: any) => {
if (VALID_FIT.includes(objectFit) && this.state.objectFit !== objectFit) {
this.setState({
objectFit,
})
}
}

componentDidUpdate(prevProps: Props) {
const {initialObjectFit} = this.props
if (initialObjectFit !== prevProps.initialObjectFit) {
this.setObjectFit(initialObjectFit)
}
}

render() {
const {objectFit} = this.state
return (
<ObjectFitContext.Provider
value={{
objectFit,
setObjectFit: this.setObjectFit,
}}
>
{this.props.children}
</ObjectFitContext.Provider>
)
}
}
export default ObjectFitProvider

0 comments on commit 8dea133

Please sign in to comment.