Skip to content

Commit 8be5f73

Browse files
committed
wip
1 parent 9498cd0 commit 8be5f73

File tree

3 files changed

+18
-69
lines changed

3 files changed

+18
-69
lines changed

packages/emotion/src/styleUtils/calcFocusOutlineStyles.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ const calcFocusOutlineStyles = (
5353
focusPosition?: 'offset' | 'inset'
5454
shouldAnimateFocus?: boolean
5555
focusWithin?: boolean
56+
withFocusOutline?: boolean
5657
}
5758
) => {
5859
const focusColor = params?.focusColor ?? 'info'
5960
const focusPosition = params?.focusPosition ?? 'offset'
6061
const shouldAnimateFocus = params?.shouldAnimateFocus ?? true
6162
const focusWithin = params?.focusWithin ?? false
63+
const withFocusOutline = params?.withFocusOutline ?? false
6264

6365
const focusColorVariants = {
6466
info: theme.infoColor,
@@ -80,6 +82,7 @@ const calcFocusOutlineStyles = (
8082
outlineOffset: '-0.8rem',
8183
outlineStyle: 'solid',
8284
outlineColor: alpha(outlineStyle.outlineColor, 0),
85+
...(withFocusOutline && outlineStyle),
8386
'&:focus': {
8487
...outlineStyle,
8588
'&:hover, &:active': {

packages/emotion/src/styleUtils/calcMarginFromShorthand.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ export function calcMarginFromShorthand(
6565
value: Spacing | undefined,
6666
spacingMap: DeepStringRecord
6767
) {
68-
if (value === undefined) {
69-
return '0'
70-
}
68+
if (!value) return
69+
7170
const tokens = value.trim().split(' ')
7271

7372
// Map each token to its resolved CSS value

packages/ui-view/src/View/styles.ts

Lines changed: 13 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ import { DIRECTION } from '@instructure/ui-i18n'
2626
import {
2727
calcMarginFromShorthand,
2828
mirrorShorthandEdges,
29-
mirrorShorthandCorners
29+
mirrorShorthandCorners,
30+
calcFocusOutlineStyles
3031
} from '@instructure/emotion'
3132
import { pickProps } from '@instructure/ui-react-utils'
3233

3334
import type { OtherHTMLAttributes } from '@instructure/shared-types'
3435
import type { NewComponentTypes, SharedTokens } from '@instructure/ui-themes'
3536
import type { ViewProps, ViewStyle, BorderColor } from './props'
36-
import { alpha } from '@instructure/ui-color-utils'
3737
import { elevationTokenToBoxShadow } from '@instructure/ui-themes'
3838

3939
const 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

Comments
 (0)