Skip to content

Commit

Permalink
chore(react): remove share and full-screen
Browse files Browse the repository at this point in the history
  • Loading branch information
vanbasten17 committed Dec 17, 2021
1 parent 90f2a26 commit 9f9e526
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
32 changes: 30 additions & 2 deletions packages/botonic-react/src/components/image.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'react-photoswipe/lib/photoswipe.css'

import { INPUT, isBrowser } from '@botonic/core'
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'
import { PhotoSwipe } from 'react-photoswipe'
import styled from 'styled-components'

import { ROLES } from '../constants'
import { Portal } from '../webchat/components/portal-modal'
import { Message } from './message'
import { getMeta } from './size-checker'

const StyledImage = styled.img`
border-radius: 8px;
Expand All @@ -23,7 +24,28 @@ const serialize = imageProps => {

export const Image = props => {
let content = props.children

const [isGalleryOpened, setIsGalleryOpened] = useState(false)
const [imageDimensions, setImageDimensions] = useState({})
useEffect(() => {
// Remove fullscreen and share buttons
if (isGalleryOpened) {
const fullScreenButton = document.getElementsByClassName(
'pswp__button pswp__button--fs'
)[0]
fullScreenButton.remove()
const shareButton = document.getElementsByClassName(
'pswp__button pswp__button--share'
)[0]
shareButton.remove()
}
}, [isGalleryOpened])

useEffect(async () => {
if (props.src !== undefined) {
setImageDimensions(await getMeta(props.src))
}
}, [props.src])

if (isBrowser()) {
content = (
Expand All @@ -33,7 +55,13 @@ export const Image = props => {
<Portal>
<PhotoSwipe
isOpen={true}
items={[{ src: props.src, w: 1200, h: 900 }]}
items={[
{
src: props.src,
w: imageDimensions.width,
h: imageDimensions.height,
},
]}
options={{ bgOpacity: 0.7 }}
onClose={() => setIsGalleryOpened(false)}
/>
Expand Down
12 changes: 12 additions & 0 deletions packages/botonic-react/src/components/size-checker.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function getMeta(url) {
try {
return new Promise((resolve, reject) => {
const img = new Image()
img.onload = () => resolve({ height: img.height, width: img.width })
img.onerror = reject
img.src = url
})
} catch (e) {
console.error(e)
}
}

0 comments on commit 9f9e526

Please sign in to comment.