UNPKG

@atlaskit/image

Version:

An image that changes in light or dark themes.

54 lines (53 loc) • 2.19 kB
/* index.tsx generated by @compiled/babel-plugin v3.0.2 */ import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["src", "srcDark", "alt", "testId", "className"]; import "./index.compiled.css"; import * as React from 'react'; import { ax, ix } from "@compiled/react/runtime"; import { useEffect, useRef } from 'react'; import { UNSAFE_useColorModeForMigration } from '@atlaskit/app-provider/use-color-mode-for-migration'; import { useThemeObserver } from '@atlaskit/tokens/use-theme-observer'; var baseImageStyles = null; /** * __Image__ * * This component can be used interchangeably with the native `img` element. It includes additional functionality, such as theme support. * * - [Examples](https://atlassian.design/components/image/examples) * - [Code](https://atlassian.design/components/image/code) * - [Usage](https://atlassian.design/components/image/usage) */ export default function Image(_ref) { var src = _ref.src, srcDark = _ref.srcDark, alt = _ref.alt, testId = _ref.testId, className = _ref.className, props = _objectWithoutProperties(_ref, _excluded); var imgRef = useRef(null); var providedColorMode = UNSAFE_useColorModeForMigration(); var _useThemeObserver = useThemeObserver(), observedColorMode = _useThemeObserver.colorMode; var colorMode = providedColorMode || observedColorMode; useEffect(function () { if (imgRef === null || imgRef.current === null) { return; } if (srcDark && colorMode === 'dark') { imgRef.current.src = srcDark; } else if (src) { imgRef.current.src = src; } }, [src, srcDark, colorMode]); return /*#__PURE__*/React.createElement("img", _extends({ alt: alt, "data-testid": testId, src: src, // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop className: ax(["_p12f1osq _4t3i1wug", className]), ref: imgRef // The spread operator is necessary since the component can accept all the props of an `img` element. // eslint-disable-next-line @repo/internal/react/no-unsafe-spread-props }, props)); }