Content.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { useContext, useMemo, PropsWithChildren } from 'react'
  21. import { ContainerContext } from '../ContainerContext'
  22. import { SlideContext } from './SlideContext'
  23. const SlideContent: React.RefForwardingComponent<
  24. HTMLDivElement,
  25. PropsWithChildren<{}>
  26. > = (props, ref) => {
  27. const { slideParams } = useContext(SlideContext)
  28. const { scale, slideTranslate } = useContext(ContainerContext)
  29. const slideStyle = useMemo(() => {
  30. const [translateX, translateY] = slideTranslate
  31. const cssStyle: React.CSSProperties = {
  32. width: `${slideParams.width}px`,
  33. height: `${slideParams.height}px`,
  34. transform: `translate(${translateX}%, ${translateY}%) scale(${
  35. scale[0]
  36. }, ${scale[1]})`
  37. }
  38. const backgroundStyle: React.CSSProperties = {
  39. backgroundSize: 'cover'
  40. }
  41. const { backgroundColor, backgroundImage, scaleMode } = slideParams
  42. if (backgroundColor) {
  43. const rgb = backgroundColor.join()
  44. backgroundStyle.backgroundColor = `rgba(${rgb})`
  45. }
  46. if (backgroundImage) {
  47. backgroundStyle.backgroundImage = `url("${backgroundImage}")`
  48. }
  49. // to adjust full screen style in mobile
  50. const setStyleToBody =
  51. scaleMode === 'scaleWidth' && window.screen.width <= 1024
  52. Object.entries(backgroundStyle).forEach(([key, value]) => {
  53. setStyleToBody
  54. ? (document.body.style[key] = value)
  55. : (cssStyle[key] = value)
  56. })
  57. return cssStyle
  58. }, [slideParams, scale, slideTranslate])
  59. return (
  60. <div ref={ref} className="display-slide" style={slideStyle}>
  61. {props.children}
  62. </div>
  63. )
  64. }
  65. export default React.forwardRef(SlideContent)