Skip to content

Commit

Permalink
fix: disable button focus style in Space.Compact (ant-design#39157)
Browse files Browse the repository at this point in the history
* fix: button hover style in Space.Compact

* fix: button hover style in Space.Compact

* style: disable button focus effect when in compact-item

* chore: update comments

* fix: improve key name

* fix: remove focus style
  • Loading branch information
foryuki authored Dec 7, 2022
1 parent 7641b2f commit cccbab7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
6 changes: 3 additions & 3 deletions components/button/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const genSharedButtonStyle: GenerateStyle<ButtonToken, CSSObject> = (token): CSS
...genFocusStyle(token),
},

...genCompactItemStyle(token, componentCls),
...genCompactItemStyle(token, componentCls, { focus: false }),
...genCompactItemVerticalStyle(token, componentCls),

// make `btn-icon-only` not too narrow
Expand All @@ -69,7 +69,7 @@ const genSharedButtonStyle: GenerateStyle<ButtonToken, CSSObject> = (token): CSS
display: 'inline-block',
width: token.lineWidth,
height: `calc(100% + ${token.lineWidth * 2}px)`,
backgroundColor: token.colorPrimaryBorder,
backgroundColor: token.colorPrimaryHover,
content: '""',
},
},
Expand All @@ -87,7 +87,7 @@ const genSharedButtonStyle: GenerateStyle<ButtonToken, CSSObject> = (token): CSS
display: 'inline-block',
width: `calc(100% + ${token.lineWidth * 2}px)`,
height: token.lineWidth,
backgroundColor: token.colorPrimaryBorder,
backgroundColor: token.colorPrimaryHover,
content: '""',
},
},
Expand Down
4 changes: 3 additions & 1 deletion components/date-picker/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,9 @@ const genPickerStyle: GenerateStyle<PickerToken> = (token) => {
transition: `border ${motionDurationMid}, box-shadow ${motionDurationMid}`,

// Space.Compact
...genCompactItemStyle(token, componentCls, '', `${componentCls}-focused`),
...genCompactItemStyle(token, componentCls, {
focusElCls: `${componentCls}-focused`,
}),

'&:hover, &-focused': {
...genHoverStyle(token),
Expand Down
10 changes: 4 additions & 6 deletions components/select/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,10 @@ const genSelectStyle: GenerateStyle<SelectToken> = (token) => {
width: '100%',
},
// Space.Compact
...genCompactItemStyle(
token,
componentCls,
`${componentCls}-selector`,
`${componentCls}-focused`,
),
...genCompactItemStyle(token, componentCls, {
borderElCls: `${componentCls}-selector`,
focusElCls: `${componentCls}-focused`,
}),
},
},

Expand Down
48 changes: 28 additions & 20 deletions components/style/compact-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,40 @@
import type { CSSObject } from '@ant-design/cssinjs';
import type { DerivativeToken } from '../theme/internal';

interface CompactItemOptions {
focus?: boolean;
/**
* Some component borders are implemented on child elements
* like `Select`
*/
borderElCls?: string;
/**
* Some components have special `focus` className especially with popovers
* like `Select` and `DatePicker`
*/
focusElCls?: string;
}

// handle border collapse
function compactItemBorder(
token: DerivativeToken,
borderedItemCls?: string,
popoverFocusedCls?: string,
): CSSObject {
const childCombinator = borderedItemCls ? '> *' : '';
function compactItemBorder(token: DerivativeToken, options: CompactItemOptions): CSSObject {
const childCombinator = options.borderElCls ? '> *' : '';
const hoverEffects = ['hover', options.focus ? 'focus' : null, 'active']
.filter(Boolean)
.map((n) => `&:${n} ${childCombinator}`)
.join(',');
return {
'&-item:not(&-last-item)': {
marginInlineEnd: -token.lineWidth,
},

'&-item': {
[`&:hover ${childCombinator}, &:focus ${childCombinator}, &:active ${childCombinator}`]: {
[hoverEffects]: {
zIndex: 2,
},

...(popoverFocusedCls
...(options.focusElCls
? {
[`&${popoverFocusedCls}`]: {
[`&${options.focusElCls}`]: {
zIndex: 2,
},
}
Expand All @@ -35,8 +49,8 @@ function compactItemBorder(
}

// handle border-radius
function compactItemBorderRadius(prefixCls: string, borderedElementCls?: string): CSSObject {
const childCombinator = borderedElementCls ? `> ${borderedElementCls}` : '';
function compactItemBorderRadius(prefixCls: string, options: CompactItemOptions): CSSObject {
const childCombinator = options.borderElCls ? `> ${options.borderElCls}` : '';

return {
[`&-item:not(&-first-item):not(&-last-item) ${childCombinator}`]: {
Expand Down Expand Up @@ -64,18 +78,12 @@ function compactItemBorderRadius(prefixCls: string, borderedElementCls?: string)
export function genCompactItemStyle(
token: DerivativeToken,
prefixCls: string,
/** Some component borders are implemented on child elements like `Select` */
borderedElementCls?: string,
/**
* Some components have special `focus` className especially with popovers like `Select` and
* `DatePicker`
*/
popoverFocusedCls?: string,
options: CompactItemOptions = { focus: true },
): CSSObject {
return {
'&-compact': {
...compactItemBorder(token, borderedElementCls, popoverFocusedCls),
...compactItemBorderRadius(prefixCls, borderedElementCls),
...compactItemBorder(token, options),
...compactItemBorderRadius(prefixCls, options),
},
};
}

0 comments on commit cccbab7

Please sign in to comment.