Skip to content

Commit

Permalink
feat: export helper to help appropriately build image styles
Browse files Browse the repository at this point in the history
  • Loading branch information
stropitek committed Mar 22, 2024
1 parent 721f09d commit 98b4b6c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
21 changes: 2 additions & 19 deletions src/components/TargetImage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HTMLProps, MutableRefObject } from 'react';

import { useTargetRef } from '..';
import { getTargetImageStyle } from '../libHelpers/image';

interface TargetImageProps extends Omit<HTMLProps<HTMLImageElement>, 'src'> {
src: string;
Expand All @@ -10,23 +11,5 @@ export function TargetImage(props: TargetImageProps) {
const { style, ...otherProps } = props;
const ref = useTargetRef() as MutableRefObject<HTMLImageElement>;

return (
<img
ref={ref}
{...otherProps}
// Pointer events is disabled to prevent the image to be draggable
// block display so that the container fits the dimensions of the image
style={{
display: 'block',
pointerEvents: 'none',
imageRendering: 'pixelated',
// Revert any global css rules that can influence the width and height of the image
// e.g. tailwindcss's preflight sets max-width: 100% on any img tag
// The width and height of the target should be their natural values for react-roi to work as expected
maxWidth: 'initial',
maxHeight: 'initial',
...style,
}}
/>
);
return <img ref={ref} {...otherProps} style={getTargetImageStyle(style)} />;
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export * from './hooks/useActions';
export * from './hooks/useCommittedRois';
export * from './hooks/useRoiState';
export * from './hooks/useTargetRef';

export * from './libHelpers/image';
20 changes: 20 additions & 0 deletions src/libHelpers/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { CSSProperties } from 'react';

/**
* Get img style properties to ensure proper rendering within react-roi
* @param style
*/
export function getTargetImageStyle(style?: CSSProperties): CSSProperties {
return {
// block display so that the container fits the dimensions of the image
display: 'block',
// Pointer events is disabled to prevent the image to be draggable
pointerEvents: 'none',
// Revert any global css rules that can influence the width and height of the image
// e.g. tailwindcss's preflight sets max-width: 100% on any img tag
// The width and height of the target should be their natural values for react-roi to work as expected
maxWidth: 'initial',
maxHeight: 'initial',
...style,
};
}

0 comments on commit 98b4b6c

Please sign in to comment.