Skip to content

Commit

Permalink
[Autocomplete] Apply .Mui-focused instead of data-focus on the focuse…
Browse files Browse the repository at this point in the history
…d option (#26181)
  • Loading branch information
m4theushw authored May 12, 2021
1 parent dcfff66 commit 205258b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
7 changes: 7 additions & 0 deletions docs/src/pages/guides/migration-v4/migration-v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ As the core components use emotion as a styled engine, the props used by emotion
2. `select-option` to `selectOption`
3. `remove-option` to `removeOption`

- Change the CSS rules that use `[data-focus="true"]` to use `.Mui-focused`. The `data-focus` attribute is not set on the focused option anymore, instead, global class names are used.

```diff
-'.MuiAutocomplete-option[data-focus="true"]': {
+'.MuiAutocomplete-option.Mui-focused': {
```

### Avatar

- Rename `circle` to `circular` for consistency. The possible values should be adjectives, not nouns:
Expand Down
4 changes: 2 additions & 2 deletions packages/material-ui/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ const AutocompleteListbox = experimentalStyled(
[theme.breakpoints.up('sm')]: {
minHeight: 'auto',
},
'&[data-focus="true"]': {
[`&.${autocompleteClasses.focused}`]: {
backgroundColor: theme.palette.action.hover,
// Reset on touch devices, it doesn't add specificity
'@media (hover: none)': {
Expand All @@ -354,7 +354,7 @@ const AutocompleteListbox = experimentalStyled(
},
'&[aria-selected="true"]': {
backgroundColor: alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity),
'&[data-focus="true"]': {
[`&.${autocompleteClasses.focused}`]: {
backgroundColor: alpha(
theme.palette.primary.main,
theme.palette.action.selectedOpacity + theme.palette.action.hoverOpacity,
Expand Down
2 changes: 1 addition & 1 deletion packages/material-ui/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Autocomplete, {
} from '@material-ui/core/Autocomplete';

function checkHighlightIs(listbox, expected) {
const focused = listbox.querySelector('[role="option"][data-focus]');
const focused = listbox.querySelector(`.${classes.focused}`);

if (expected) {
if (focused) {
Expand Down
6 changes: 3 additions & 3 deletions packages/material-ui/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ export default function useAutocomplete(props) {
return;
}

const prev = listboxRef.current.querySelector('[data-focus]');
const prev = listboxRef.current.querySelector('[role="option"].Mui-focused');
if (prev) {
prev.removeAttribute('data-focus');
prev.classList.remove('Mui-focused');
prev.classList.remove('Mui-focusVisible');
}

Expand All @@ -320,7 +320,7 @@ export default function useAutocomplete(props) {
return;
}

option.setAttribute('data-focus', 'true');
option.classList.add('Mui-focused');
if (reason === 'keyboard') {
option.classList.add('Mui-focusVisible');
}
Expand Down

0 comments on commit 205258b

Please sign in to comment.