@cntrl-site/components 0.1.0-alpha.26 → 0.1.0-alpha.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1639,9 +1639,11 @@ const Lightbox = ({ isOpen, onClose, content, styles: lightboxStyles, settings,
1639
1639
  const [currentIndex, setCurrentIndex] = React.useState(0);
1640
1640
  const [splideKey, setSplideKey] = React.useState(0);
1641
1641
  const [isClosing, setIsClosing] = React.useState(false);
1642
+ const [imageDimensions, setImageDimensions] = React.useState({});
1642
1643
  const lightboxRef = React.useRef(null);
1643
1644
  const prevSliderTypeRef = React.useRef(null);
1644
1645
  const imageRef = React.useRef(null);
1646
+ const imgWrapperRef = React.useRef(null);
1645
1647
  const isClosingRef = React.useRef(false);
1646
1648
  const contentRef = React.useRef(null);
1647
1649
  const animationEndHandlerRef = React.useRef(null);
@@ -1759,6 +1761,55 @@ const Lightbox = ({ isOpen, onClose, content, styles: lightboxStyles, settings,
1759
1761
  }
1760
1762
  prevSliderTypeRef.current = slider.type;
1761
1763
  }, [slider.type]);
1764
+ React.useEffect(() => {
1765
+ if (!imageRef.current || !imgWrapperRef.current || !isOpen) {
1766
+ setImageDimensions({});
1767
+ return;
1768
+ }
1769
+ const img2 = imageRef.current;
1770
+ const wrapper2 = imgWrapperRef.current;
1771
+ const currentItem = content[currentIndex];
1772
+ if ((currentItem == null ? void 0 : currentItem.image.objectFit) !== "contain") {
1773
+ setImageDimensions({});
1774
+ return;
1775
+ }
1776
+ const calculateDimensions = () => {
1777
+ if (!img2.naturalWidth || !img2.naturalHeight) return;
1778
+ const wrapperRect = wrapper2.getBoundingClientRect();
1779
+ const wrapperStyle = window.getComputedStyle(wrapper2);
1780
+ const paddingTop = parseFloat(wrapperStyle.paddingTop) || 0;
1781
+ const paddingRight = parseFloat(wrapperStyle.paddingRight) || 0;
1782
+ const paddingBottom = parseFloat(wrapperStyle.paddingBottom) || 0;
1783
+ const paddingLeft = parseFloat(wrapperStyle.paddingLeft) || 0;
1784
+ const contentWidth = wrapperRect.width - paddingLeft - paddingRight;
1785
+ const contentHeight = wrapperRect.height - paddingTop - paddingBottom;
1786
+ const containerRatio = contentWidth / contentHeight;
1787
+ const imgRatio = img2.naturalWidth / img2.naturalHeight;
1788
+ let width;
1789
+ let height;
1790
+ if (imgRatio > containerRatio) {
1791
+ width = contentWidth;
1792
+ height = contentWidth / imgRatio;
1793
+ } else {
1794
+ height = contentHeight;
1795
+ width = contentHeight * imgRatio;
1796
+ }
1797
+ setImageDimensions({ width, height });
1798
+ };
1799
+ if (img2.complete && img2.naturalWidth > 0) {
1800
+ calculateDimensions();
1801
+ } else {
1802
+ img2.onload = calculateDimensions;
1803
+ }
1804
+ const handleResize = () => calculateDimensions();
1805
+ window.addEventListener("resize", handleResize);
1806
+ return () => {
1807
+ window.removeEventListener("resize", handleResize);
1808
+ if (img2.onload) {
1809
+ img2.onload = null;
1810
+ }
1811
+ };
1812
+ }, [isOpen, currentIndex, content]);
1762
1813
  React.useEffect(() => {
1763
1814
  if (!isOpen) return;
1764
1815
  const originalOverflow = document.body.style.overflow;
@@ -1863,9 +1914,11 @@ const Lightbox = ({ isOpen, onClose, content, styles: lightboxStyles, settings,
1863
1914
  }
1864
1915
  if (e.touches.length === 0 && e.changedTouches.length > 0) {
1865
1916
  const rect = imageRef.current ? getDisplayedImageRect(imageRef.current) : null;
1917
+ console.log("rect", rect);
1866
1918
  if (!rect) return;
1867
1919
  const touch = e.changedTouches[0];
1868
1920
  const inside = touch.clientX >= rect.x && touch.clientX <= rect.x + rect.width && touch.clientY >= rect.y && touch.clientY <= rect.y + rect.height;
1921
+ console.log("inside", rect.width, rect.height);
1869
1922
  if (!inside) {
1870
1923
  e.stopPropagation();
1871
1924
  isClosingRef.current = true;
@@ -1964,13 +2017,14 @@ const Lightbox = ({ isOpen, onClose, content, styles: lightboxStyles, settings,
1964
2017
  return /* @__PURE__ */ jsxRuntime.jsx(reactSplide.SplideSlide, { children: /* @__PURE__ */ jsxRuntime.jsx(
1965
2018
  "div",
1966
2019
  {
2020
+ ref: index === currentIndex ? imgWrapperRef : null,
1967
2021
  className: styles.imgWrapper,
1968
2022
  onClick: handleImageWrapperClick,
1969
2023
  style: { padding: scalingValue(layout.padding.top, isEditor) + " " + scalingValue(layout.padding.right, isEditor) + " " + scalingValue(layout.padding.bottom, isEditor) + " " + scalingValue(layout.padding.left, isEditor) },
1970
2024
  children: /* @__PURE__ */ jsxRuntime.jsx(
1971
2025
  "img",
1972
2026
  {
1973
- ref: imageRef,
2027
+ ref: index === currentIndex ? imageRef : null,
1974
2028
  className: cn(styles.imageStyle, {
1975
2029
  [styles.contain]: item.image.objectFit === "contain",
1976
2030
  [styles.cover]: item.image.objectFit === "cover",
@@ -1979,7 +2033,15 @@ const Lightbox = ({ isOpen, onClose, content, styles: lightboxStyles, settings,
1979
2033
  src: item.image.url,
1980
2034
  alt: item.image.name ?? "",
1981
2035
  onClick: onImageClick,
1982
- style: imageStyle2
2036
+ style: {
2037
+ ...imageStyle2,
2038
+ ...item.image.objectFit === "contain" && imageDimensions.width && imageDimensions.height ? {
2039
+ width: `${imageDimensions.width}px`,
2040
+ height: `${imageDimensions.height}px`,
2041
+ maxWidth: "none",
2042
+ maxHeight: "none"
2043
+ } : {}
2044
+ }
1983
2045
  }
1984
2046
  )
1985
2047
  }
@@ -2757,8 +2819,8 @@ const LightboxComponent = {
2757
2819
  align: "center",
2758
2820
  triggers: "clk",
2759
2821
  grid: {
2760
- height: 0.03,
2761
- width: 0.03,
2822
+ height: 0.04,
2823
+ width: 0.04,
2762
2824
  gap: 8e-3
2763
2825
  },
2764
2826
  offset: { x: 0, y: 0 },
@@ -2782,7 +2844,7 @@ const LightboxComponent = {
2782
2844
  hover: "#cccccc"
2783
2845
  },
2784
2846
  area: {
2785
- color: "rgba(64,67,71,0.4)",
2847
+ color: "rgba(64,67,71,0.9)",
2786
2848
  blur: 0,
2787
2849
  closeIconUrl: null,
2788
2850
  closeIconAlign: "top-right",
package/dist/index.mjs CHANGED
@@ -1637,9 +1637,11 @@ const Lightbox = ({ isOpen, onClose, content, styles: lightboxStyles, settings,
1637
1637
  const [currentIndex, setCurrentIndex] = React.useState(0);
1638
1638
  const [splideKey, setSplideKey] = React.useState(0);
1639
1639
  const [isClosing, setIsClosing] = React.useState(false);
1640
+ const [imageDimensions, setImageDimensions] = React.useState({});
1640
1641
  const lightboxRef = useRef(null);
1641
1642
  const prevSliderTypeRef = useRef(null);
1642
1643
  const imageRef = useRef(null);
1644
+ const imgWrapperRef = useRef(null);
1643
1645
  const isClosingRef = useRef(false);
1644
1646
  const contentRef = useRef(null);
1645
1647
  const animationEndHandlerRef = useRef(null);
@@ -1757,6 +1759,55 @@ const Lightbox = ({ isOpen, onClose, content, styles: lightboxStyles, settings,
1757
1759
  }
1758
1760
  prevSliderTypeRef.current = slider.type;
1759
1761
  }, [slider.type]);
1762
+ useEffect(() => {
1763
+ if (!imageRef.current || !imgWrapperRef.current || !isOpen) {
1764
+ setImageDimensions({});
1765
+ return;
1766
+ }
1767
+ const img2 = imageRef.current;
1768
+ const wrapper2 = imgWrapperRef.current;
1769
+ const currentItem = content[currentIndex];
1770
+ if ((currentItem == null ? void 0 : currentItem.image.objectFit) !== "contain") {
1771
+ setImageDimensions({});
1772
+ return;
1773
+ }
1774
+ const calculateDimensions = () => {
1775
+ if (!img2.naturalWidth || !img2.naturalHeight) return;
1776
+ const wrapperRect = wrapper2.getBoundingClientRect();
1777
+ const wrapperStyle = window.getComputedStyle(wrapper2);
1778
+ const paddingTop = parseFloat(wrapperStyle.paddingTop) || 0;
1779
+ const paddingRight = parseFloat(wrapperStyle.paddingRight) || 0;
1780
+ const paddingBottom = parseFloat(wrapperStyle.paddingBottom) || 0;
1781
+ const paddingLeft = parseFloat(wrapperStyle.paddingLeft) || 0;
1782
+ const contentWidth = wrapperRect.width - paddingLeft - paddingRight;
1783
+ const contentHeight = wrapperRect.height - paddingTop - paddingBottom;
1784
+ const containerRatio = contentWidth / contentHeight;
1785
+ const imgRatio = img2.naturalWidth / img2.naturalHeight;
1786
+ let width;
1787
+ let height;
1788
+ if (imgRatio > containerRatio) {
1789
+ width = contentWidth;
1790
+ height = contentWidth / imgRatio;
1791
+ } else {
1792
+ height = contentHeight;
1793
+ width = contentHeight * imgRatio;
1794
+ }
1795
+ setImageDimensions({ width, height });
1796
+ };
1797
+ if (img2.complete && img2.naturalWidth > 0) {
1798
+ calculateDimensions();
1799
+ } else {
1800
+ img2.onload = calculateDimensions;
1801
+ }
1802
+ const handleResize = () => calculateDimensions();
1803
+ window.addEventListener("resize", handleResize);
1804
+ return () => {
1805
+ window.removeEventListener("resize", handleResize);
1806
+ if (img2.onload) {
1807
+ img2.onload = null;
1808
+ }
1809
+ };
1810
+ }, [isOpen, currentIndex, content]);
1760
1811
  useEffect(() => {
1761
1812
  if (!isOpen) return;
1762
1813
  const originalOverflow = document.body.style.overflow;
@@ -1861,9 +1912,11 @@ const Lightbox = ({ isOpen, onClose, content, styles: lightboxStyles, settings,
1861
1912
  }
1862
1913
  if (e.touches.length === 0 && e.changedTouches.length > 0) {
1863
1914
  const rect = imageRef.current ? getDisplayedImageRect(imageRef.current) : null;
1915
+ console.log("rect", rect);
1864
1916
  if (!rect) return;
1865
1917
  const touch = e.changedTouches[0];
1866
1918
  const inside = touch.clientX >= rect.x && touch.clientX <= rect.x + rect.width && touch.clientY >= rect.y && touch.clientY <= rect.y + rect.height;
1919
+ console.log("inside", rect.width, rect.height);
1867
1920
  if (!inside) {
1868
1921
  e.stopPropagation();
1869
1922
  isClosingRef.current = true;
@@ -1962,13 +2015,14 @@ const Lightbox = ({ isOpen, onClose, content, styles: lightboxStyles, settings,
1962
2015
  return /* @__PURE__ */ jsx(SplideSlide, { children: /* @__PURE__ */ jsx(
1963
2016
  "div",
1964
2017
  {
2018
+ ref: index === currentIndex ? imgWrapperRef : null,
1965
2019
  className: styles.imgWrapper,
1966
2020
  onClick: handleImageWrapperClick,
1967
2021
  style: { padding: scalingValue(layout.padding.top, isEditor) + " " + scalingValue(layout.padding.right, isEditor) + " " + scalingValue(layout.padding.bottom, isEditor) + " " + scalingValue(layout.padding.left, isEditor) },
1968
2022
  children: /* @__PURE__ */ jsx(
1969
2023
  "img",
1970
2024
  {
1971
- ref: imageRef,
2025
+ ref: index === currentIndex ? imageRef : null,
1972
2026
  className: cn(styles.imageStyle, {
1973
2027
  [styles.contain]: item.image.objectFit === "contain",
1974
2028
  [styles.cover]: item.image.objectFit === "cover",
@@ -1977,7 +2031,15 @@ const Lightbox = ({ isOpen, onClose, content, styles: lightboxStyles, settings,
1977
2031
  src: item.image.url,
1978
2032
  alt: item.image.name ?? "",
1979
2033
  onClick: onImageClick,
1980
- style: imageStyle2
2034
+ style: {
2035
+ ...imageStyle2,
2036
+ ...item.image.objectFit === "contain" && imageDimensions.width && imageDimensions.height ? {
2037
+ width: `${imageDimensions.width}px`,
2038
+ height: `${imageDimensions.height}px`,
2039
+ maxWidth: "none",
2040
+ maxHeight: "none"
2041
+ } : {}
2042
+ }
1981
2043
  }
1982
2044
  )
1983
2045
  }
@@ -2755,8 +2817,8 @@ const LightboxComponent = {
2755
2817
  align: "center",
2756
2818
  triggers: "clk",
2757
2819
  grid: {
2758
- height: 0.03,
2759
- width: 0.03,
2820
+ height: 0.04,
2821
+ width: 0.04,
2760
2822
  gap: 8e-3
2761
2823
  },
2762
2824
  offset: { x: 0, y: 0 },
@@ -2780,7 +2842,7 @@ const LightboxComponent = {
2780
2842
  hover: "#cccccc"
2781
2843
  },
2782
2844
  area: {
2783
- color: "rgba(64,67,71,0.4)",
2845
+ color: "rgba(64,67,71,0.9)",
2784
2846
  blur: 0,
2785
2847
  closeIconUrl: null,
2786
2848
  closeIconAlign: "top-right",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntrl-site/components",
3
- "version": "0.1.0-alpha.26",
3
+ "version": "0.1.0-alpha.27",
4
4
  "description": "Custom components for control editor and public websites.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",