UNPKG

@atlaskit/image

Version:

An image that changes in light or dark themes.

54 lines (53 loc) • 1.88 kB
/* index.tsx generated by @compiled/babel-plugin v3.0.2 */ import _extends from "@babel/runtime/helpers/extends"; 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'; const 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({ src, srcDark, alt, testId, className, ...props }) { const imgRef = useRef(null); const providedColorMode = UNSAFE_useColorModeForMigration(); const { colorMode: observedColorMode } = useThemeObserver(); const colorMode = providedColorMode || observedColorMode; useEffect(() => { 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)); }