Skip to content

Commit

Permalink
feat: added focus styles for interactive elements (#576)
Browse files Browse the repository at this point in the history
* feat: adding focus styles for interactive elements
  • Loading branch information
Kyzyl-ool authored Oct 18, 2023
1 parent e058713 commit 4c6dd96
Show file tree
Hide file tree
Showing 39 changed files with 231 additions and 98 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
},
"peerDependencies": {
"@doc-tools/transform": "^3.3.2",
"@gravity-ui/uikit": "^5.4.1",
"@gravity-ui/uikit": "^5.12.0",
"react": "^16.0.0 || ^17.0.0 || ^18.0.0"
},
"devDependencies": {
Expand All @@ -120,7 +120,7 @@
"@gravity-ui/prettier-config": "^1.0.1",
"@gravity-ui/stylelint-config": "^1.0.0",
"@gravity-ui/tsconfig": "^1.0.0",
"@gravity-ui/uikit": "^5.9.1",
"@gravity-ui/uikit": "^5.12.2",
"@storybook/addon-actions": "^7.1.0",
"@storybook/addon-essentials": "^7.1.0",
"@storybook/addon-knobs": "^7.0.2",
Expand Down
6 changes: 6 additions & 0 deletions src/blocks/ContentLayout/ContentLayout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ $block: '.#{$ns}content-layout-block';
padding: $indentXL;
}

&_theme {
&_dark {
--g-color-line-focus: var(--pc-color-line-focus-dark);
}
}

@media (max-width: map-get($gridBreakpoints, 'sm')) {
&_background {
padding: $indentM;
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/ContentLayout/ContentLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const ContentLayoutBlock = (props: ContentLayoutBlockProps) => {
const colSizes = useMemo(() => getTextWidth(textWidth), [textWidth]);

return (
<div className={b({size, background: Boolean(background)})}>
<div className={b({size, theme, background: Boolean(background)})}>
<Content
className={b('content')}
{...textContent}
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/Icons/Icons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ $block: '.#{$ns}icons-block';
@include reset-link-style();
margin: 0 $indentXXXS $indentSM;
}
a#{$block}__item {
@include focusable();
border-radius: var(--g-focus-border-radius);
}

&__image {
max-width: 100%;
Expand Down
13 changes: 13 additions & 0 deletions src/blocks/Questions/QuestionBlockItem/QuestionBlockItem.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ $block: '.#{$ns}QuestionsBlockItem';

&__title {
@include heading4();
@include focusable();

position: relative;
padding-right: 24px;
cursor: pointer;
border-radius: var(--g-focus-border-radius);

a {
@include link();
Expand All @@ -27,11 +29,22 @@ $block: '.#{$ns}QuestionsBlockItem';
position: absolute;
right: 0;
top: 0;
bottom: 0;
margin: auto;
color: var(--g-color-text-primary);
}

&__link {
@include text-size(body-2);

@include add-specificity(&) {
a {
outline-offset: -2px; // as part of outline is hidden due to overflow: hidden from parent
border-radius: calc(
var(--g-focus-border-radius) + 2px
); // as outline-offset is -2px
}
}
}

&__text {
Expand Down
2 changes: 2 additions & 0 deletions src/blocks/Slider/Arrow/Arrow.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ $block: '.#{$ns}slider-block-arrow';
box-shadow: 0 4px 24px var(--pc-color-sfx-shadow), 0 2px 8px var(--pc-color-sfx-shadow);

transition: box-shadow 0.3s $ease-out-cubic, color 0.3s $ease-out-cubic;

@include focusable();
}

&:hover {
Expand Down
26 changes: 17 additions & 9 deletions src/blocks/Slider/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export const SliderBlock = (props: WithChildren<SliderProps>) => {
return (
// To have this key differ from keys used in renderDot function, added `-accessible-bar` part
<Fragment key={`${index}-accessible-bar`}>
{slidesCountByBreakpoint > 1 && (
{slidesCountByBreakpoint > 0 && (
<li
className={b('accessible-bar')}
aria-current
Expand All @@ -266,8 +266,9 @@ export const SliderBlock = (props: WithChildren<SliderProps>) => {
);
};

const renderDot = (index: number) => {
const getCurrentSlideNumber = (index: number) => {
const currentIndexDiff = index - currentIndex;

let currentSlideNumber;
if (0 <= currentIndexDiff && currentIndexDiff < slidesToShowCount) {
currentSlideNumber = currentIndex + 1;
Expand All @@ -276,19 +277,26 @@ export const SliderBlock = (props: WithChildren<SliderProps>) => {
} else {
currentSlideNumber = index + 1;
}
return currentSlideNumber;
};
const isVisibleSlide = (index: number) => {
const currentIndexDiff = index - currentIndex;

return (
slidesCountByBreakpoint > 0 &&
0 <= currentIndexDiff &&
currentIndexDiff < slidesToShowCount
);
};

const renderDot = (index: number) => {
return (
<li
key={index}
className={b('dot', {active: index === currentIndex})}
onClick={() => handleDotClick(index)}
aria-hidden={
(slidesCountByBreakpoint > 1 &&
0 <= currentIndexDiff &&
currentIndexDiff < slidesToShowCount) ||
undefined
}
aria-label={`Slide ${currentSlideNumber} of ${barSlidesCount}`}
aria-hidden={isVisibleSlide(index) ? true : undefined}
aria-label={`Slide ${getCurrentSlideNumber(index)} of ${barSlidesCount}`}
></li>
);
};
Expand Down
31 changes: 7 additions & 24 deletions src/blocks/Slider/__tests__/Slider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,13 @@ describe('Slider', () => {
const barDotsCount = CARDS_COUNT - slidesToShow + 1;

// Checking labels for the first slide
if (slidesToShow > 1) {
// There we have a bar covering `slidesToShow` dots
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const accessibleBarElement = container.querySelector('.pc-SliderBlock__accessible-bar');
expect(accessibleBarElement?.getAttribute('aria-label')).toBe(
`Slide 1 of ${barDotsCount}`,
);
expect(
queryHelpers.queryAllByAttribute(
'aria-label',
container,
`Slide 1 of ${barDotsCount}`,
),
).toHaveLength(slidesToShow + 1);
} else {
// There is no bar covering dots
expect(
queryHelpers.queryAllByAttribute(
'aria-label',
container,
`Slide 1 of ${barDotsCount}`,
),
).toHaveLength(1);
}
// There we have a bar covering `slidesToShow` dots
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
const accessibleBarElement = container.querySelector('.pc-SliderBlock__accessible-bar');
expect(accessibleBarElement?.getAttribute('aria-label')).toBe(`Slide 1 of ${barDotsCount}`);
expect(
queryHelpers.queryAllByAttribute('aria-label', container, `Slide 1 of ${barDotsCount}`),
).toHaveLength(slidesToShow + 1);

// Checking labels for the slides starting from 2
Array(barDotsCount - 1)
Expand Down
9 changes: 9 additions & 0 deletions src/components/Button/Button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
$block: '.#{$ns}button-block';

#{$block} {
--yc-button-outline-color: var(--g-color-line-focus);

&__content {
display: flex;
align-items: center;
Expand Down Expand Up @@ -34,6 +36,13 @@ $block: '.#{$ns}button-block';
&_monochrome {
@include monochrome-button();
}

&_normal-contrast,
&_raised {
&:focus::before {
outline-offset: 1px;
}
}
}

&_size {
Expand Down
3 changes: 3 additions & 0 deletions src/components/ButtonTabs/ButtonTabs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ $block: '.#{$ns}button-tabs';
var(--pc-selected-tab-item-background-color),
$hoverBackgroundColor: var(--pc-selected-tab-item-background-color-hover)
);
&:focus::before {
outline-offset: 1px;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ButtonTabs/ButtonTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const ButtonTabs: React.FC<ButtonTabsProps> = ({
size={tabSize}
onClick={handleClick(id)}
extraProps={{
'aria-current': isActive,
'aria-current': isActive || undefined,
}}
/>
);
Expand Down
17 changes: 11 additions & 6 deletions src/components/CardBase/CardBase.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import React, {Children, Fragment, HTMLAttributeAnchorTarget, ReactElement} from 'react';

import {Link} from '@gravity-ui/uikit';

import {useAnalytics} from '../../hooks';
import {useMetrika} from '../../hooks/useMetrika';
import {
AnalyticsEventsBase,
ButtonPixel,
CardBaseProps as CardBaseParams,
DefaultEventNames,
ImageProps,
MetrikaGoal,
WithChildren,
} from '../../models';
import {AnalyticsEventsBase, DefaultEventNames} from '../../models/common';
import {block, getQaAttrubutes} from '../../utils';
import BackgroundImage from '../BackgroundImage/BackgroundImage';
import RouterLink from '../RouterLink/RouterLink';
Expand Down Expand Up @@ -120,19 +123,21 @@ export const Layout = (props: CardBaseProps) => {

return url ? (
<RouterLink href={url}>
<a
<Link
href={url}
target={target}
rel={target === '_blank' ? 'noopener noreferrer' : undefined}
className={fullClassName}
draggable={false}
onDragStart={(e) => e.preventDefault()}
onClick={onClick}
title={urlTitle}
data-qa={qa}
extraProps={{
draggable: false,
onDragStart: (e: React.DragEvent<HTMLAnchorElement>) => e.preventDefault(),
}}
qa={qa}
>
{cardContent}
</a>
</Link>
</RouterLink>
) : (
<div className={fullClassName} data-qa={qa}>
Expand Down
3 changes: 2 additions & 1 deletion src/components/Control/Control.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ $block: '.#{$ns}control';
display: flex;
justify-content: center;
align-items: center;

border-radius: var(--g-focus-border-radius);
transition: color $transitionTime;

@include reset-button-style();
@include islands-focus();
@include focusable();

&_size {
&_xs {
Expand Down
2 changes: 2 additions & 0 deletions src/components/FileLink/FileLink.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ $block: '.#{$ns}file-link';

&__link > a {
color: var(--g-color-text-primary);
border-radius: var(--g-focus-border-radius);
@include focusable();
}

&__link > a:hover {
Expand Down
11 changes: 10 additions & 1 deletion src/components/FullscreenImage/FullscreenImage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ $closeButtonSize: 36px;

&-wrapper {
position: relative;

&:hover {
#{$block}__icon-wrapper {
opacity: 1;
}
}
}
}

Expand All @@ -31,6 +37,9 @@ $closeButtonSize: 36px;
}

&__icon-wrapper {
@include reset-button-style();
@include focusable();

display: flex;
align-items: center;
justify-content: center;
Expand All @@ -45,7 +54,7 @@ $closeButtonSize: 36px;
opacity: 0;
transition: 0.3s;

&_visible {
&:focus {
opacity: 1;
}
}
Expand Down
Loading

0 comments on commit 4c6dd96

Please sign in to comment.