Skip to content

Commit fcdbca9

Browse files
authored
refactor(region): Use buttons for better borders and drop shadows (#496)
1 parent 1a4d879 commit fcdbca9

20 files changed

+278
-334
lines changed

scripts/build_locale.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ const { execSync } = require('child_process');
99
*/
1010
module.exports = (locale = 'en-US', callback) => {
1111
try {
12-
console.log(`Building ${locale}`);
12+
console.log(`Building ${locale}`); // eslint-disable-line
1313
// build assets for a single locale
1414
execSync(`time LANGUAGE=${locale} yarn build:prod:dist`);
1515
callback();
1616
} catch (error) {
17-
console.error(`Error: Failed to build ${locale}`);
17+
console.error(`Error: Failed to build ${locale}`); // eslint-disable-line
1818
callback(true);
1919
}
2020
};

src/common/BaseAnnotator.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
@import '~box-ui-elements/es/styles/common/forms';
2121
@import '~box-ui-elements/es/styles/common/buttons';
2222

23+
.ba-Layer {
24+
@include box-sizing;
25+
@include common-typography;
26+
}
27+
2328
&.is-hidden {
2429
.ba-Layer {
2530
display: none;

src/components/AnnotationTarget/AnnotationTarget.tsx

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/components/AnnotationTarget/__tests__/AnnotationTarget-test.tsx

Lines changed: 0 additions & 97 deletions
This file was deleted.

src/components/AnnotationTarget/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/region/RegionAnnotation.scss

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
@import '~box-ui-elements/es/styles/variables';
2+
@import './RegionRect';
3+
14
.ba-RegionAnnotation {
2-
&.is-active,
3-
&:focus,
5+
@include ba-RegionRect;
6+
7+
margin: 0;
8+
padding: 0;
9+
background: none;
10+
border: none;
11+
box-shadow: none;
12+
413
&:hover {
5-
outline: none;
614
cursor: pointer;
15+
}
716

8-
.ba-RegionRect {
9-
stroke-width: 4px;
10-
}
17+
&:focus {
18+
outline: none;
1119
}
1220
}

src/region/RegionAnnotation.tsx

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,50 @@
1-
import classNames from 'classnames';
21
import * as React from 'react';
3-
import AnnotationTarget from '../components/AnnotationTarget';
4-
import RegionRect from './RegionRect';
5-
import { Rect } from '../@types';
2+
import classNames from 'classnames';
3+
import noop from 'lodash/noop';
4+
import { Shape, styleShape } from './regionUtil';
65
import './RegionAnnotation.scss';
76

87
type Props = {
98
annotationId: string;
109
className?: string;
1110
isActive?: boolean;
1211
onSelect?: (annotationId: string) => void;
13-
shape: Rect;
12+
shape: Shape;
1413
};
1514

16-
const RegionAnnotation = (props: Props, ref: React.Ref<HTMLAnchorElement>): JSX.Element => {
17-
const { isActive, shape, ...rest } = props;
18-
const className = classNames('ba-RegionAnnotation', { 'is-active': isActive });
15+
export type RegionAnnotationRef = HTMLButtonElement;
16+
17+
export const RegionAnnotation = (props: Props, ref: React.Ref<RegionAnnotationRef>): JSX.Element => {
18+
const { annotationId, className, isActive, onSelect = noop, shape } = props;
19+
20+
const cancelEvent = (event: React.SyntheticEvent): void => {
21+
event.preventDefault();
22+
event.stopPropagation();
23+
event.nativeEvent.stopImmediatePropagation(); // Prevents document event handlers from executing
24+
};
25+
const handleBlur = (event: React.SyntheticEvent): void => {
26+
cancelEvent(event);
27+
};
28+
const handleClick = (event: React.MouseEvent): void => {
29+
cancelEvent(event);
30+
onSelect(annotationId);
31+
};
32+
const handleFocus = (event: React.SyntheticEvent): void => {
33+
cancelEvent(event);
34+
onSelect(annotationId);
35+
};
1936

2037
return (
21-
<AnnotationTarget ref={ref} className={className} isActive={isActive} {...rest}>
22-
<RegionRect {...shape} />
23-
</AnnotationTarget>
38+
<button
39+
ref={ref}
40+
className={classNames('ba-RegionAnnotation', className, { 'is-active': isActive })}
41+
data-testid={`ba-AnnotationTarget-${annotationId}`}
42+
onBlur={handleBlur}
43+
onClick={handleClick}
44+
onFocus={handleFocus}
45+
style={styleShape(shape)}
46+
type="button"
47+
/>
2448
);
2549
};
2650

src/region/RegionAnnotations.scss

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@
66
left: 0;
77
width: 100%;
88
height: 100%;
9-
filter: drop-shadow(0 2px 3px rgba(0, 0, 0, .25));
10-
}
11-
12-
.ba-RegionAnnotations-creator,
13-
.ba-RegionAnnotations-target {
14-
.ba-RegionRect {
15-
stroke-width: 4px;
16-
}
179
}
1810

1911
.ba-RegionAnnotations-creator {
@@ -25,7 +17,7 @@
2517
pointer-events: none;
2618

2719
&.is-listening {
28-
.ba-AnnotationTarget {
20+
.ba-RegionAnnotation {
2921
pointer-events: auto; // Delegate event control to avoid re-rendering every target on mousedown/up
3022
}
3123
}

src/region/RegionAnnotations.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as React from 'react';
33
import PopupReply from '../components/Popups/PopupReply';
44
import RegionCreator from './RegionCreator';
55
import RegionList from './RegionList';
6-
import RegionRect from './RegionRect';
6+
import RegionRect, { RegionRectRef } from './RegionRect';
77
import { AnnotationRegion, Rect } from '../@types';
88
import { CreatorItem, CreatorStatus } from '../store/creator';
99
import { scaleShape } from './regionUtil';
@@ -24,7 +24,7 @@ type Props = {
2424
};
2525

2626
type State = {
27-
rectRef?: SVGRectElement;
27+
rectRef?: RegionRectRef;
2828
};
2929

3030
export default class RegionAnnotations extends React.PureComponent<Props, State> {
@@ -83,7 +83,7 @@ export default class RegionAnnotations extends React.PureComponent<Props, State>
8383
createRegion(staged);
8484
};
8585

86-
setRectRef = (rectRef: SVGRectElement): void => {
86+
setRectRef = (rectRef: RegionRectRef): void => {
8787
this.setState({ rectRef });
8888
};
8989

@@ -117,9 +117,9 @@ export default class RegionAnnotations extends React.PureComponent<Props, State>
117117

118118
{/* Layer 3a: Staged (unsaved) annotation target, if any */}
119119
{isCreating && staged && (
120-
<svg className="ba-RegionAnnotations-target">
121-
<RegionRect ref={this.setRectRef} {...scaleShape(staged.shape, scale)} />
122-
</svg>
120+
<div className="ba-RegionAnnotations-target">
121+
<RegionRect ref={this.setRectRef} isActive shape={scaleShape(staged.shape, scale)} />
122+
</div>
123123
)}
124124

125125
{/* Layer 3b: Staged (unsaved) annotation description popup, if 3a is ready */}

src/region/RegionCreator.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ $region_cursor_32_3x: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAY
88
}
99

1010
.ba-RegionCreator-rect {
11-
will-change: height, left, top, width;
11+
will-change: height, transform, width;
1212
}

0 commit comments

Comments
 (0)