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: be possible to customize placeholder #278

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions packages/react/src/components/Card/CardEmpty.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import { emptyStateAnimation, emptyStateImageAnimation } from './CardAnimation'
import CardImage from './CardMedia/Image'
import { Content } from './CardContent'
import { GlobalContext } from '../../context/GlobalState'
import { isLarge, isSmall } from '../../utils'
import { classNames, isLarge, isSmall } from '../../utils'

const MediaEmpty = styled(CardImage)`
const Placeholder = styled.span.attrs({
className: classNames.placeholderContent
})``

const MediaEmpty = styled(CardImage).attrs({ className: classNames.placeholderMedia })`
${emptyStateImageAnimation};
`

const HeaderEmpty = styled('span')`
const HeaderEmpty = styled(Placeholder)`
opacity: 0.8;
height: 16px;
width: ${({ cardSize }) => (!isSmall(cardSize) ? '60%' : '75%')};
Expand All @@ -30,7 +34,7 @@ const HeaderEmpty = styled('span')`
`};
`

const DescriptionEmpty = styled('span')`
const DescriptionEmpty = styled(Placeholder)`
opacity: 0.8;
height: 14px;
width: 95%;
Expand All @@ -40,12 +44,13 @@ const DescriptionEmpty = styled('span')`
animation-delay: 0.125s;
`

const FooterEmpty = styled('span')`
const FooterEmpty = styled(Placeholder)`
opacity: 0.8;
height: 12px;
width: 30%;
display: block;
${emptyStateAnimation} animation-delay: .25s;
${emptyStateAnimation};
animation-delay: 0.25s;

${({ cardSize }) =>
!isLarge(cardSize) &&
Expand Down
19 changes: 15 additions & 4 deletions packages/react/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { useIntersectionObserver } from './utils/hooks'
const Card = props => {
const {
className,
components,
fetchData,
lazy,
loading,
Expand All @@ -53,6 +54,10 @@ const Card = props => {
[mediaProps, props]
)

const { PlaceholderComponent = CardEmpty } = components

const isLoading = isLoadingUndefined ? loadingState : loading

const isLazyEnabled = useMemo(
() => isLazySupported && (lazy === true || isObject(lazy)),
[lazy]
Expand All @@ -68,6 +73,12 @@ const Card = props => {
[isLazyEnabled, hasIntersected]
)

const cardWrapClassName = useMemo(() => {
const base = `${classNames.main} ${isLoading ? classNames.placeholder : ''}`.trim()

return `${base} ${className}`.trim()
}, [className, isLoading])

const mergeData = useCallback(
fetchedData => {
const payload = isFunction(setData)
Expand Down Expand Up @@ -158,8 +169,6 @@ microlink.io/${error.code.toLowerCase()}

useEffect(toFetchData, [url, setData, hasIntersected])

const isLoading = isLoadingUndefined ? loadingState : loading

if (isError) {
return (
<a href={url} {...restProps}>
Expand Down Expand Up @@ -191,14 +200,14 @@ microlink.io/${error.code.toLowerCase()}

return (
<CardWrap
className={`${classNames.main} ${className}`.trim()}
className={cardWrapClassName}
href={url}
isLoading={isLoading}
ref={cardRef}
{...restProps}
>
{isLoading ? (
<CardEmpty />
<PlaceholderComponent />
) : (
<>
<CardMedia />
Expand All @@ -217,6 +226,7 @@ Microlink.defaultProps = {
className: '',
apiKey: undefined,
autoPlay: true,
components: { PlaceholderComponent: CardEmpty },
controls: true,
direction: 'ltr',
lazy: true,
Expand All @@ -231,6 +241,7 @@ Microlink.defaultProps = {
Microlink.propTypes = {
apiKey: PropTypes.string,
autoPlay: PropTypes.bool,
components: PropTypes.shape({ PlaceholderComponent: PropTypes.elementType }),
contrast: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
controls: PropTypes.bool,
direction: PropTypes.string,
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const BASE_CLASSNAME = 'microlink_card'
const CONTENT_BASE_CLASSNAME = `${BASE_CLASSNAME}__content`
const MEDIA_BASE_CLASSNAME = `${BASE_CLASSNAME}__media`
const CONTROLS_BASE_CLASSNAME = `${MEDIA_BASE_CLASSNAME}__controls`
const PLACEHOLDER_BASE_CLASSNAME = `${BASE_CLASSNAME}__placeholder`

export const classNames = {
main: BASE_CLASSNAME,
Expand All @@ -144,5 +145,8 @@ export const classNames = {
progressBar: `${CONTROLS_BASE_CLASSNAME}_progress_bar`,
progressTime: `${CONTROLS_BASE_CLASSNAME}_progress_time`,
spinner: `${CONTROLS_BASE_CLASSNAME}_spinner`,
iframe: `${BASE_CLASSNAME}__iframe`
iframe: `${BASE_CLASSNAME}__iframe`,
placeholder: PLACEHOLDER_BASE_CLASSNAME,
placeholderMedia: `${PLACEHOLDER_BASE_CLASSNAME}_media`,
placeholderContent: `${PLACEHOLDER_BASE_CLASSNAME}_content`
}
Loading