-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(LEMS-2664): update use control point and movable point with optional props #1906
Changes from 6 commits
7eb2761
83839b5
4b4236c
f01b2b4
059baef
8bcc3b7
7c19062
0ec45c5
8cf8bbd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/perseus": patch | ||
--- | ||
|
||
update moveable point component and use control point method to have optional params |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,18 @@ import * as React from "react"; | |
import {useControlPoint} from "./use-control-point"; | ||
|
||
import type {CSSCursor} from "./css-cursor"; | ||
import type {AriaLive} from "../../types"; | ||
import type {KeyboardMovementConstraint} from "../use-draggable"; | ||
import type {vec} from "mafs"; | ||
|
||
type Props = { | ||
point: vec.Vector2; | ||
ariaDescribedBy?: string; | ||
ariaLabel?: string; | ||
ariaLive?: AriaLive; | ||
color?: string; | ||
constrain?: KeyboardMovementConstraint; | ||
cursor?: CSSCursor | undefined; | ||
/** | ||
* Represents where this point stands in the overall point sequence. | ||
* This is used to provide screen readers with context about the point. | ||
|
@@ -16,14 +23,11 @@ type Props = { | |
* Note: This number is 1-indexed, and should restart from 1 for each | ||
* interactive figure on the graph. | ||
*/ | ||
sequenceNumber: number; | ||
onMove?: (newPoint: vec.Vector2) => unknown; | ||
sequenceNumber?: number; | ||
onBlur?: ((event: React.FocusEvent) => unknown) | undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to explicitly have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, I kept them as they were, but it would be safe to remove the |
||
onClick?: () => unknown; | ||
color?: string; | ||
cursor?: CSSCursor | undefined; | ||
constrain?: KeyboardMovementConstraint; | ||
onFocus?: ((event: React.FocusEvent) => unknown) | undefined; | ||
onBlur?: ((event: React.FocusEvent) => unknown) | undefined; | ||
onMove?: (newPoint: vec.Vector2) => unknown; | ||
}; | ||
|
||
export const MovablePoint = React.forwardRef( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,13 +10,20 @@ import {useDraggable} from "../use-draggable"; | |
import {MovablePointView} from "./movable-point-view"; | ||
|
||
import type {CSSCursor} from "./css-cursor"; | ||
import type {AriaLive} from "../../types"; | ||
import type {KeyboardMovementConstraint} from "../use-draggable"; | ||
import type {vec} from "mafs"; | ||
|
||
type Params = { | ||
point: vec.Vector2; | ||
ariaDescribedBy?: string; | ||
ariaLabel?: string; | ||
ariaLive?: AriaLive; | ||
color?: string | undefined; | ||
cursor?: CSSCursor | undefined; | ||
constrain?: KeyboardMovementConstraint; | ||
// The focusableHandle element is assigned to the forwarded ref. | ||
forwardedRef?: React.ForwardedRef<SVGGElement | null> | undefined; | ||
/** | ||
* Represents where this point stands in the overall point sequence. | ||
* This is used to provide screen readers with context about the point. | ||
|
@@ -25,14 +32,11 @@ type Params = { | |
* Note: This number is 1-indexed, and should restart from 1 for each | ||
* interactive figure on the graph. | ||
*/ | ||
sequenceNumber: number; | ||
constrain?: KeyboardMovementConstraint; | ||
sequenceNumber?: number; | ||
onMove?: ((newPoint: vec.Vector2) => unknown) | undefined; | ||
onClick?: (() => unknown) | undefined; | ||
onFocus?: ((event: React.FocusEvent) => unknown) | undefined; | ||
onBlur?: ((event: React.FocusEvent) => unknown) | undefined; | ||
// The focusableHandle element is assigned to the forwarded ref. | ||
forwardedRef?: React.ForwardedRef<SVGGElement | null> | undefined; | ||
}; | ||
|
||
type Return = { | ||
|
@@ -46,15 +50,18 @@ export function useControlPoint(params: Params): Return { | |
const {snapStep, disableKeyboardInteraction} = useGraphConfig(); | ||
const { | ||
point, | ||
sequenceNumber, | ||
ariaDescribedBy, | ||
ariaLabel, | ||
ariaLive = "polite", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only removing point or moving out of bounds should be |
||
color, | ||
cursor, | ||
constrain = (p) => snap(snapStep, p), | ||
forwardedRef = noop, | ||
sequenceNumber = 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
onMove = noop, | ||
onClick = noop, | ||
onFocus = noop, | ||
onBlur = noop, | ||
forwardedRef = noop, | ||
} = params; | ||
|
||
const {strings, locale} = usePerseusI18n(); | ||
|
@@ -76,6 +83,15 @@ export function useControlPoint(params: Params): Return { | |
constrainKeyboardMovement: constrain, | ||
}); | ||
|
||
// if custom aria label is not provided, will use default of sequence number and point coordinates | ||
const pointAriaLabel = | ||
ariaLabel || | ||
strings.srPointAtCoordinates({ | ||
num: sequenceNumber, | ||
anakaren-rojas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
x: srFormatNumber(point[X], locale), | ||
y: srFormatNumber(point[Y], locale), | ||
}); | ||
|
||
useLayoutEffect(() => { | ||
setForwardedRef(forwardedRef, focusableHandleRef.current); | ||
}, [forwardedRef]); | ||
|
@@ -87,14 +103,9 @@ export function useControlPoint(params: Params): Return { | |
tabIndex={disableKeyboardInteraction ? -1 : 0} | ||
ref={focusableHandleRef} | ||
role="button" | ||
aria-label={strings.srPointAtCoordinates({ | ||
num: sequenceNumber, | ||
x: srFormatNumber(point[X], locale), | ||
y: srFormatNumber(point[Y], locale), | ||
})} | ||
// aria-live="assertive" causes the new location of the point to be | ||
// announced immediately on move. | ||
aria-live="assertive" | ||
aria-describedby={ariaDescribedBy} | ||
aria-label={pointAriaLabel} | ||
aria-live={ariaLive} | ||
onFocus={(event) => { | ||
onFocus(event); | ||
setFocused(true); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice tests! 🎉