forked from zhihu/griffith
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use hooks in ObjectFitProvider
- Loading branch information
Showing
3 changed files
with
31 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |