-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(target-size): update to match new spacing requirements (#4117)
* fix(target-size): update to match new spacing requirements * working * tests * finalize? * tests * revert playground * 🤖 Automated formatting fixes * Apply suggestions from code review Co-authored-by: Wilco Fiers <WilcoFiers@users.noreply.github.com> * 🤖 Automated formatting fixes * udpate tests * dont half minOffset but double return from getOffset * Apply suggestions from code review Co-authored-by: Wilco Fiers <WilcoFiers@users.noreply.github.com> * 🤖 Automated formatting fixes * fix tests * fix test --------- Co-authored-by: straker <straker@users.noreply.github.com> Co-authored-by: Wilco Fiers <WilcoFiers@users.noreply.github.com>
- Loading branch information
1 parent
fcf76e0
commit 49eaa0e
Showing
12 changed files
with
359 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import findNearbyElms from './find-nearby-elms'; | ||
import { splitRects, hasVisualOverlap } from '../math'; | ||
import memoize from '../../core/utils/memoize'; | ||
|
||
const roundingMargin = 0.05; | ||
|
||
export default memoize(getTargetSize); | ||
|
||
/** | ||
* Compute the target size of an element. | ||
* @see https://www.w3.org/TR/WCAG22/#dfn-targets | ||
*/ | ||
function getTargetSize(vNode, minSize) { | ||
const nodeRect = vNode.boundingClientRect; | ||
const overlappingVNodes = findNearbyElms(vNode).filter(vNeighbor => { | ||
return ( | ||
vNeighbor.getComputedStylePropertyValue('pointer-events') !== 'none' && | ||
hasVisualOverlap(vNode, vNeighbor) | ||
); | ||
}); | ||
|
||
if (!overlappingVNodes.length) { | ||
return nodeRect; | ||
} | ||
|
||
return getLargestUnobscuredArea(vNode, overlappingVNodes, minSize); | ||
} | ||
|
||
// Find areas of the target that are not obscured | ||
function getLargestUnobscuredArea(vNode, obscuredNodes, minSize) { | ||
const nodeRect = vNode.boundingClientRect; | ||
if (obscuredNodes.length === 0) { | ||
return null; | ||
} | ||
const obscuringRects = obscuredNodes.map( | ||
({ boundingClientRect: rect }) => rect | ||
); | ||
const unobscuredRects = splitRects(nodeRect, obscuringRects); | ||
if (!unobscuredRects.length) { | ||
return null; | ||
} | ||
|
||
// Of the unobscured inner rects, work out the largest | ||
return getLargestRect(unobscuredRects, minSize); | ||
} | ||
|
||
// Find the largest rectangle in the array, prioritize ones that meet a minimum size | ||
function getLargestRect(rects, minSize) { | ||
return rects.reduce((rectA, rectB) => { | ||
const rectAisMinimum = rectHasMinimumSize(minSize, rectA); | ||
const rectBisMinimum = rectHasMinimumSize(minSize, rectB); | ||
// Prioritize rects that pass the minimum | ||
if (rectAisMinimum !== rectBisMinimum) { | ||
return rectAisMinimum ? rectA : rectB; | ||
} | ||
const areaA = rectA.width * rectA.height; | ||
const areaB = rectB.width * rectB.height; | ||
return areaA > areaB ? rectA : rectB; | ||
}); | ||
} | ||
|
||
function rectHasMinimumSize(minSize, { width, height }) { | ||
return ( | ||
width + roundingMargin >= minSize && height + roundingMargin >= minSize | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.