Skip to content

Commit

Permalink
feat: add components prop
Browse files Browse the repository at this point in the history
+ Add new prop for injecting custom loading component
+ Update stories
  • Loading branch information
breadadams committed Jun 21, 2022
1 parent 6963ad6 commit 66d5768
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
6 changes: 3 additions & 3 deletions packages/react/src/components/Card/CardEmpty.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ 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 Placeholder = styled.span.attrs({
className: 'microlink_card_placeholder'
className: classNames.placeholderContent
})``

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

Expand Down
21 changes: 15 additions & 6 deletions packages/react/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ import { useIntersectionObserver } from './utils/hooks'
const Card = props => {
const {
className,
components,
fetchData,
lazy,
loading,
media: mediaProp,
setData,
url,
apiKey, // destructuring to avoid pass it
placeholderComponent: CardEmpty,
...restProps
} = props

Expand All @@ -54,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 @@ -69,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 toFetchData = useCallback(() => {
if (canFetchData) {
setLoading(true)
Expand Down Expand Up @@ -167,8 +177,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 @@ -200,14 +208,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 @@ -225,8 +233,8 @@ const Microlink = props => (
Microlink.defaultProps = {
className: '',
apiKey: undefined,
placeholderComponent: CardEmpty,
autoPlay: true,
components: { PlaceholderComponent: CardEmpty },
controls: true,
direction: 'ltr',
lazy: true,
Expand All @@ -241,6 +249,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 @@ -114,6 +114,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 @@ -137,5 +138,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`
}
11 changes: 11 additions & 0 deletions packages/react/stories/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'unfetch/polyfill'

import React from 'react'
import { storiesOf } from '@storybook/react'

import { urls, urlsVideo, urlsAudio, urlsIframe } from './data'
Expand Down Expand Up @@ -207,3 +208,13 @@ storiesOf('style', module)
})
)
)

const PlaceholderComponent = () => <strong style={{ height: '100%', width: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>loading...</strong>

storiesOf('components', module)
.add('PlaceholderComponent', () =>
createStoryEntry({
loading: true,
fetchData: false,
components: { PlaceholderComponent }
}))

0 comments on commit 66d5768

Please sign in to comment.