Skip to content

Commit

Permalink
[Components - ToggleGroupControl]: Fix visual state when no option is…
Browse files Browse the repository at this point in the history
… selected (#35545)

* [Components - ToggleGroupControl]: Fix visual state when no option is selected

* don't render backdrop if no target node exists

* change initial state value
  • Loading branch information
ntsekouras authored Oct 14, 2021
1 parent 0160b7f commit a304131
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,6 @@ exports[`ToggleGroupControl should render correctly 1`] = `
}
.emotion-6 {
background: #1e1e1e;
border-radius: 2px;
box-shadow: transparent;
left: 0;
position: absolute;
top: 2px;
bottom: 2px;
-webkit-transition: -webkit-transform 160ms ease;
transition: transform 160ms ease;
z-index: 1;
}
@media ( prefers-reduced-motion: reduce ) {
.emotion-6 {
transition-duration: 0ms;
}
}
.emotion-8 {
display: -webkit-inline-box;
display: -webkit-inline-flex;
display: -ms-inline-flexbox;
Expand All @@ -80,7 +61,7 @@ exports[`ToggleGroupControl should render correctly 1`] = `
flex: 1;
}
.emotion-10 {
.emotion-8 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
Expand Down Expand Up @@ -119,20 +100,20 @@ exports[`ToggleGroupControl should render correctly 1`] = `
}
@media ( prefers-reduced-motion: reduce ) {
.emotion-10 {
.emotion-8 {
transition-duration: 0ms;
}
}
.emotion-10::-moz-focus-inner {
.emotion-8::-moz-focus-inner {
border: 0;
}
.emotion-10:active {
.emotion-8:active {
background: #fff;
}
.emotion-11 {
.emotion-9 {
font-size: 13px;
line-height: 1;
position: absolute;
Expand All @@ -144,7 +125,7 @@ exports[`ToggleGroupControl should render correctly 1`] = `
transform: translate( -50%, -50% );
}
.emotion-13 {
.emotion-11 {
font-size: 13px;
font-weight: bold;
height: 0;
Expand Down Expand Up @@ -182,17 +163,12 @@ exports[`ToggleGroupControl should render correctly 1`] = `
/>
<div
class="emotion-6 emotion-7"
role="presentation"
style="transform: translateX(0px); transition: none; width: 0px;"
/>
<div
class="emotion-8 emotion-9"
data-active="false"
>
<button
aria-checked="false"
aria-label="R"
class="emotion-10 components-toggle-group-control-option"
class="emotion-8 components-toggle-group-control-option"
data-value="rigas"
data-wp-c16t="true"
data-wp-component="ToggleGroupControlOption"
Expand All @@ -201,26 +177,26 @@ exports[`ToggleGroupControl should render correctly 1`] = `
tabindex="0"
>
<div
class="emotion-11 emotion-12"
class="emotion-9 emotion-10"
>
R
</div>
<div
aria-hidden="true"
class="emotion-13 emotion-14"
class="emotion-11 emotion-12"
>
R
</div>
</button>
</div>
<div
class="emotion-8 emotion-9"
class="emotion-6 emotion-7"
data-active="false"
>
<button
aria-checked="false"
aria-label="J"
class="emotion-10 components-toggle-group-control-option"
class="emotion-8 components-toggle-group-control-option"
data-value="jack"
data-wp-c16t="true"
data-wp-component="ToggleGroupControlOption"
Expand All @@ -229,13 +205,13 @@ exports[`ToggleGroupControl should render correctly 1`] = `
tabindex="-1"
>
<div
class="emotion-11 emotion-12"
class="emotion-9 emotion-10"
>
J
</div>
<div
aria-hidden="true"
class="emotion-13 emotion-14"
class="emotion-11 emotion-12"
>
J
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function ToggleGroupControlBackdrop( {
const [ left, setLeft ] = useState( 0 );
const [ width, setWidth ] = useState( 0 );
const [ canAnimate, setCanAnimate ] = useState( false );
const [ renderBackdrop, setRenderBackdrop ] = useState( false );

useEffect( () => {
const containerNode = containerRef?.current;
Expand All @@ -29,7 +30,10 @@ function ToggleGroupControlBackdrop( {
const targetNode = containerNode.querySelector(
`[data-value="${ state }"]`
);
if ( ! targetNode ) return;
setRenderBackdrop( !! targetNode );
if ( ! targetNode ) {
return;
}

const { x: parentX } = containerNode.getBoundingClientRect();
const { width: offsetWidth, x } = targetNode.getBoundingClientRect();
Expand All @@ -48,6 +52,10 @@ function ToggleGroupControlBackdrop( {
return () => window.cancelAnimationFrame( requestId );
}, [ canAnimate, containerRef, containerWidth, state, isAdaptiveWidth ] );

if ( ! renderBackdrop ) {
return null;
}

return (
<BackdropView
role="presentation"
Expand Down

0 comments on commit a304131

Please sign in to comment.