{"version":3,"file":"image.cjs","sources":["../src/image.ts"],"sourcesContent":["import { useEffect, useRef, useState } from \"react\";\nimport h from \"@macrostrat/hyper\";\n\ntype ConfinedImageProps = {\n  maxHeight?: number | null;\n  maxWidth?: number | null;\n  src: string;\n};\n\ntype ConfinedImageSize = {\n  width: number;\n  height: number;\n};\n\nexport function ConfinedImage(props: ConfinedImageProps) {\n  let { maxHeight, maxWidth, src } = props;\n  const [imageSize, setImageSize] = useState<ConfinedImageSize>(null);\n\n  const imgRef = useRef(null);\n\n  useEffect(() => {\n    const img = imgRef.current;\n    if (img != null) {\n      img.onload = () => {\n        const height = img.naturalHeight;\n        const width = img.naturalWidth;\n        setImageSize({ height, width });\n      };\n    }\n  }, [imgRef]);\n\n  if (maxHeight == null) {\n    maxHeight = 200;\n  }\n  if (maxWidth == null) {\n    maxWidth = 200;\n  }\n  if (imageSize != null) {\n    if (maxHeight > imageSize.height) {\n      maxHeight = imageSize.height;\n    }\n    if (maxWidth > imageSize.width) {\n      maxWidth = imageSize.width;\n    }\n  }\n\n  const imgStyle = {\n    maxHeight,\n    maxWidth,\n  };\n\n  const style = { maxHeight, maxWidth };\n  return h(\"div.image-container\", { style }, [\n    h(\"img\", { src, ref: imgRef, style: imgStyle }),\n  ]);\n}\n"],"names":["useState","useRef","useEffect","h"],"mappings":";;;;;;AAcO,SAAS,cAAc,OAA2B;AACvD,MAAI,EAAE,WAAW,UAAU,IAAA,IAAQ;AACnC,QAAM,CAAC,WAAW,YAAY,IAAIA,MAAAA,SAA4B,IAAI;AAElE,QAAM,SAASC,MAAAA,OAAO,IAAI;AAE1BC,QAAAA,UAAU,MAAM;AACd,UAAM,MAAM,OAAO;AACnB,QAAI,OAAO,MAAM;AACf,UAAI,SAAS,MAAM;AACjB,cAAM,SAAS,IAAI;AACnB,cAAM,QAAQ,IAAI;AAClB,qBAAa,EAAE,QAAQ,OAAO;AAAA,MAChC;AAAA,IACF;AAAA,EACF,GAAG,CAAC,MAAM,CAAC;AAEX,MAAI,aAAa,MAAM;AACrB,gBAAY;AAAA,EACd;AACA,MAAI,YAAY,MAAM;AACpB,eAAW;AAAA,EACb;AACA,MAAI,aAAa,MAAM;AACrB,QAAI,YAAY,UAAU,QAAQ;AAChC,kBAAY,UAAU;AAAA,IACxB;AACA,QAAI,WAAW,UAAU,OAAO;AAC9B,iBAAW,UAAU;AAAA,IACvB;AAAA,EACF;AAEA,QAAM,WAAW;AAAA,IACf;AAAA,IACA;AAAA,EAAA;AAGF,QAAM,QAAQ,EAAE,WAAW,SAAA;AAC3B,SAAOC,mBAAE,uBAAuB,EAAE,SAAS;AAAA,IACzCA,WAAAA,QAAE,OAAO,EAAE,KAAK,KAAK,QAAQ,OAAO,UAAU;AAAA,EAAA,CAC/C;AACH;;"}