Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

fix(Embed): get rid of white flash #1909

Merged
merged 18 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Fix `CreateShorthandOptions` should be typed @lucivpav ([#1886](https://github.com/stardust-ui/react/pull/1886))
- Add `shadowLevel1Dark` in Teams themes @notandrew ([#1887](https://github.com/stardust-ui/react/pull/1887))
- When merging themes use deep merge for site and component variables @miroslavstastny ([#1907](https://github.com/stardust-ui/react/pull/1907))
- Fix white flash when activating `Embed` component @lucivpav ([#1909](https://github.com/stardust-ui/react/pull/1909))

### Features
- Add `TextArea` component @lucivpav ([#1897](https://github.com/stardust-ui/react/pull/1897))
Expand Down
40 changes: 26 additions & 14 deletions packages/react/src/components/Embed/Embed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { embedBehavior } from '../../lib/accessibility'
import { Accessibility } from '../../lib/accessibility/types'
import Icon, { IconProps } from '../Icon/Icon'
import Image, { ImageProps } from '../Image/Image'
import Image from '../Image/Image'
import Video, { VideoProps } from '../Video/Video'
import Box, { BoxProps } from '../Box/Box'
import { ComponentEventHandler, WithAsProp, ShorthandValue, withSafeTypeForAs } from '../../types'
Expand Down Expand Up @@ -55,14 +55,15 @@ export interface EmbedProps extends UIComponentProps {
onClick?: ComponentEventHandler<EmbedProps>

/** Image source URL for when video isn't playing. */
placeholder?: ShorthandValue<ImageProps>
placeholder?: string

/** Shorthand for an embedded video. */
video?: ShorthandValue<VideoProps>
}

export interface EmbedState {
active: boolean
iframeLoaded: boolean
}

class Embed extends AutoControlledComponent<WithAsProp<EmbedProps>, EmbedState> {
Expand Down Expand Up @@ -104,7 +105,7 @@ class Embed extends AutoControlledComponent<WithAsProp<EmbedProps>, EmbedState>
}

getInitialAutoControlledState(): EmbedState {
return { active: false }
return { active: false, iframeLoaded: false }
}

handleClick = e => {
Expand All @@ -123,8 +124,15 @@ class Embed extends AutoControlledComponent<WithAsProp<EmbedProps>, EmbedState>

renderComponent({ ElementType, classes, accessibility, unhandledProps, styles, variables }) {
const { control, iframe, placeholder, video } = this.props
const { active } = this.state
const { active, iframeLoaded } = this.state
const controlVisible = !_.isNil(video) || !active
const placeholderImage = placeholder ? (
<Image
src={placeholder}
styles={styles.image}
variables={{ width: variables.width, height: variables.height }}
/>
) : null

return (
<ElementType
Expand All @@ -134,6 +142,7 @@ class Embed extends AutoControlledComponent<WithAsProp<EmbedProps>, EmbedState>
{...unhandledProps}
{...applyAccessibilityKeyHandlers(accessibility.keyHandlers.root, unhandledProps)}
>
{iframe && active && !iframeLoaded && placeholderImage}
lucivpav marked this conversation as resolved.
Show resolved Hide resolved
{active ? (
<>
{Video.create(video, {
Expand All @@ -142,25 +151,28 @@ class Embed extends AutoControlledComponent<WithAsProp<EmbedProps>, EmbedState>
controls: false,
loop: true,
muted: true,
poster: placeholder,
styles: styles.video,
variables: {
width: variables.width,
height: variables.height,
},
},
})}
{Box.create(iframe, { defaultProps: { as: 'iframe', styles: styles.iframe } })}
{Box.create(iframe, {
defaultProps: {
as: 'iframe',
styles: styles.iframe,
},
overrideProps: {
onLoad: () => {
this.setState({ iframeLoaded: true })
lucivpav marked this conversation as resolved.
Show resolved Hide resolved
},
},
})}
</>
) : (
Image.create(placeholder, {
defaultProps: {
styles: styles.image,
variables: {
width: variables.width,
height: variables.height,
},
},
})
placeholderImage
)}

{controlVisible &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export default {
top: '50%',
transform: 'translate(-50%, -50%)',
}),
iframe: (): ICSSInJSStyle => ({
iframe: ({ props: p }): ICSSInJSStyle => ({
display: 'block',
visibility: p.iframeLoaded ? 'visible' : 'hidden',
}),
} as ComponentSlotStylesPrepared<EmbedProps & EmbedState, EmbedVariables>