Skip to content

Commit

Permalink
Use ID as a namespace to increase specificity
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 authored and ramonjd committed Dec 30, 2024
1 parent cedef1c commit 3b1affe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function presetRatioAsNumber( { ratio, ...rest } ) {

export default function AspectRatioDropdown( { toggleProps } ) {
const {
state: { image, cropper },
state: { image, cropper, isAspectRatioLocked },
dispatch,
} = useImageCropper();
const defaultAspect = image.width / image.height;
Expand Down Expand Up @@ -89,7 +89,7 @@ export default function AspectRatioDropdown( { toggleProps } ) {
}
onClose();
} }
value={ aspectRatio }
value={ isAspectRatioLocked ? aspectRatio : 0 }
aspectRatios={ [
// All ratios should be mirrored in AspectRatioTool in @wordpress/block-editor.
{
Expand Down
11 changes: 11 additions & 0 deletions packages/components/src/image-cropper/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ const Cropper = forwardRef< HTMLDivElement >( ( {}, ref ) => {

return (
<MaxWidthWrapper
// Disable reason: We're using this ID as a namespace only to increase CSS specificity for this component.
// eslint-disable-next-line no-restricted-syntax
id="components-image-cropper"
style={ {
width: isAxisSwapped
? `${ originalHeight }px`
Expand Down Expand Up @@ -232,6 +235,10 @@ const Cropper = forwardRef< HTMLDivElement >( ( {}, ref ) => {
crossOrigin="anonymous"
isResizing={ isResizing }
isDragging={ isDragging }
style={ {
width: `${ image.width }px`,
height: `${ image.height }px`,
} }
/>
<CropWindow>
<ContainWindow>
Expand All @@ -242,6 +249,10 @@ const Cropper = forwardRef< HTMLDivElement >( ( {}, ref ) => {
alt=""
crossOrigin="anonymous"
ref={ imageRef }
style={ {
width: `${ image.width }px`,
height: `${ image.height }px`,
} }
/>
</ContainWindow>
</CropWindow>
Expand Down
33 changes: 19 additions & 14 deletions packages/components/src/image-cropper/styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export const Resizable = styled( MotionResizable )`
export const MaxWidthWrapper = styled.div`
position: relative;
max-width: 100%;
min-width: 0;
`;

Expand All @@ -115,19 +114,25 @@ export const ContainWindow = styled.div`
`;

export const Img = styled( motion.img )`
position: absolute;
pointer-events: none;
top: 50%;
left: 50%;
transform-origin: center center;
rotate: var( --wp-cropper-angle );
scale: var( --wp-cropper-scale-x ) var( --wp-cropper-scale-y );
translate: calc(
var( --wp-cropper-image-x ) - var( --wp-cropper-window-x ) - 50%
)
calc( var( --wp-cropper-image-y ) - var( --wp-cropper-window-y ) - 50% );
will-change: rotate, scale, translate;
contain: strict;
// Using a "namespace" ID to increase CSS specificity for this component.
#components-image-cropper & {
position: absolute;
pointer-events: none;
top: 50%;
left: 50%;
transform-origin: center center;
rotate: var( --wp-cropper-angle );
scale: var( --wp-cropper-scale-x ) var( --wp-cropper-scale-y );
translate: calc(
var( --wp-cropper-image-x ) - var( --wp-cropper-window-x ) - 50%
)
calc(
var( --wp-cropper-image-y ) - var( --wp-cropper-window-y ) - 50%
);
will-change: rotate, scale, translate;
contain: strict;
max-width: none;
}
`;

export const BackgroundImg = styled( Img, {
Expand Down

0 comments on commit 3b1affe

Please sign in to comment.