@@ -26,14 +26,14 @@ import { DIRECTION } from '@instructure/ui-i18n'
2626import {
2727 calcMarginFromShorthand ,
2828 mirrorShorthandEdges ,
29- mirrorShorthandCorners
29+ mirrorShorthandCorners ,
30+ calcFocusOutlineStyles
3031} from '@instructure/emotion'
3132import { pickProps } from '@instructure/ui-react-utils'
3233
3334import type { OtherHTMLAttributes } from '@instructure/shared-types'
3435import type { NewComponentTypes , SharedTokens } from '@instructure/ui-themes'
3536import type { ViewProps , ViewStyle , BorderColor } from './props'
36- import { alpha } from '@instructure/ui-color-utils'
3737import { elevationTokenToBoxShadow } from '@instructure/ui-themes'
3838
3939const processBorderRadiusValue = (
@@ -219,66 +219,6 @@ const withBorder = (props: ViewProps) => {
219219 return borderWidth && borderWidth !== '0' && borderWidth !== 'none'
220220}
221221
222- const getFocusStyles = (
223- props : ViewProps ,
224- componentTheme : NewComponentTypes [ 'View' ]
225- ) => {
226- const {
227- focusColor,
228- focusPosition,
229- shouldAnimateFocus,
230- withFocusOutline,
231- focusWithin
232- } = props
233-
234- const focusPos =
235- focusPosition == 'offset' || focusPosition == 'inset'
236- ? focusPosition
237- : 'offset'
238- const focusPositionVariants = {
239- offset : {
240- outlineOffset : `calc(${ componentTheme . focusOutlineOffset } - ${ componentTheme . focusOutlineWidth } )`
241- } ,
242- inset : {
243- outlineOffset : `calc(${ componentTheme . focusOutlineInset } - ${ componentTheme . focusOutlineWidth } )`
244- }
245- }
246- const focusColorVariants = {
247- info : componentTheme . focusColorInfo ,
248- inverse : componentTheme . focusColorInverse ,
249- success : componentTheme . focusColorSuccess ,
250- danger : componentTheme . focusColorDanger
251- }
252- const visibleFocusStyle = {
253- ...focusPositionVariants [ focusPos ] ,
254- outlineColor : focusColorVariants [ focusColor ! ]
255- }
256- const outlineStyle = {
257- outlineOffset : '-0.8rem' , // value when not in focus, its invisible
258- outlineColor : alpha ( focusColorVariants [ focusColor ! ] , 0 ) ,
259- outlineStyle : componentTheme . focusOutlineStyle ,
260- outlineWidth : componentTheme . focusOutlineWidth ,
261- ...( withFocusOutline && visibleFocusStyle )
262- }
263- return {
264- ...( shouldAnimateFocus && {
265- transition : 'outline-color 0.2s, outline-offset 0.25s'
266- } ) ,
267- ...outlineStyle ,
268- '&:hover, &:active' : {
269- // apply the same style so it's not overridden by some global style
270- ...outlineStyle
271- } ,
272- ...( typeof withFocusOutline === 'undefined' && {
273- // user focuses the element
274- '&:focus' : visibleFocusStyle
275- } ) ,
276- ...( focusWithin && {
277- '&:focus-within' : visibleFocusStyle
278- } )
279- }
280- }
281-
282222/**
283223 * Generates the style object from the theme and provided additional information
284224 * @param {Object } componentTheme The theme variable object.
@@ -473,7 +413,14 @@ const generateStyle = (
473413 none : { }
474414 }
475415
476- const focusStyles = getFocusStyles ( props , componentTheme )
416+ const { focusColor, focusPosition, shouldAnimateFocus, withFocusOutline } =
417+ props
418+ const focusOutline = calcFocusOutlineStyles ( sharedTokens . focusOutline , {
419+ focusColor,
420+ focusPosition,
421+ shouldAnimateFocus,
422+ withFocusOutline
423+ } )
477424 return {
478425 view : {
479426 label : 'view' ,
@@ -509,16 +456,16 @@ const generateStyle = (
509456 //every '&' symbol will add another class to the rule, so it will be stronger
510457 //making an accidental override less likely
511458 '&&&&&&&&&&' : {
512- ...spacingStyle ,
513459 ...offsetStyle ,
514- ...focusStyles ,
460+ ...focusOutline ,
515461 width,
516462 height,
517463 minWidth,
518464 minHeight,
519465 maxWidth,
520466 maxHeight,
521- ...getStyleProps ( props )
467+ ...getStyleProps ( props ) ,
468+ ...spacingStyle
522469 }
523470 }
524471 }
0 commit comments