Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2201] Toast Component added with locales #2210

Merged
merged 24 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ GEM
ruby2_keywords (~> 0.0.1)
netrc (0.11.0)
nio4r (2.5.8)
nokogiri (1.13.9)
nokogiri (1.13.10)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
oauth2 (1.4.9)
Expand Down
4 changes: 0 additions & 4 deletions app/assets/stylesheets/core/alerts.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
text-align: center;
border-radius: $size-4;

&Dashboard {
@include setMargin($size-0, $size-0, $size-26, $size-0);
}

&Text {
color: $carmine;
}
Expand Down
4 changes: 0 additions & 4 deletions app/assets/stylesheets/core/notices.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@
padding: $size-10;
text-align: center;
border-radius: $size-4;

&Dashboard {
@include setMargin($size-0, $size-0, $size-26, $size-0);
}
}
5 changes: 4 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
<%= react_component('SkipToContent', props: { id: 'mainContent' })%>
<%= react_component('Header', props: header_props) %>
<main>
<%= render partial: 'shared/alerts' %>
<%= react_component('Toast', props: {
notice: notice.present? ? notice : '',
alert: alert.present? ? alert : '' ,
appendDashboardClass: (user_signed_in? && !static_page?) || secret_share_path? }) %>
<% if user_signed_in? && !static_page? %>
<div class="dashboardContent">
<div class="dashboardNav" role="navigation" aria-label="<%= t('navigation.user_menu') %>">
Expand Down
8 changes: 0 additions & 8 deletions app/views/shared/_alerts.html.erb

This file was deleted.

6 changes: 4 additions & 2 deletions client/app/components/Input/Input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@

&Label {
@include setMargin($size-0, $size-0, $size-0, $size-8);

color: $white;
julianguyen marked this conversation as resolved.
Show resolved Hide resolved
text-transform: capitalize;
}
}

.clr-white {
zeeshansarwar38 marked this conversation as resolved.
Show resolved Hide resolved
color: $white;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this class? I don't see it being used anywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged

.default,
.tagAutocomplete,
.password,
Expand Down
2 changes: 1 addition & 1 deletion client/app/components/Input/InputCheckbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const InputCheckbox = (props: Props): Node => {
{typeof uncheckedValue !== 'undefined'
&& displayUnchecked(name, uncheckedValue)}
{displayCheckbox(id, name, value, checked, onChange, label)}
<div className={css.checkboxLabel}>{renderHTML(label)}</div>
<label className={`${css.checkboxLabel}`} htmlFor={id}>{renderHTML(label)}</label>
zeeshansarwar38 marked this conversation as resolved.
Show resolved Hide resolved
</div>
{displayInfo(info)}
</div>
Expand Down
19 changes: 19 additions & 0 deletions client/app/components/Toast/Toast.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@import "~styles/_global.scss";

.toast {
display: flex;
justify-content: space-between;
width: fit-content;
margin: 0 auto;

button {
background: transparent;
julianguyen marked this conversation as resolved.
Show resolved Hide resolved
color: $white;
opacity: 0.5;
border: 0;
&:hover {
border: 0;
opacity: 1;
}
}
}
zeeshansarwar38 marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 29 additions & 0 deletions client/app/components/Toast/__tests__/Toast.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @flow
import React from 'react';
import { render, screen } from '@testing-library/react';
import { Toast } from 'components/Toast';

describe('Toast', () => {
zeeshansarwar38 marked this conversation as resolved.
Show resolved Hide resolved
describe('Toast Type: Alert', () => {
it('renders correctly', () => {
render(
<Toast
alert="Invalid username or password."
appendDashboardClass="true"
/>,
);
expect(screen).not.toBeNull();
});
});

describe('Toast Type: Notice', () => {
it('renders correctly', () => {
render(
<Toast
notice="Login successful."
/>,
);
expect(screen).not.toBeNull();
});
});
});
zeeshansarwar38 marked this conversation as resolved.
Show resolved Hide resolved
73 changes: 73 additions & 0 deletions client/app/components/Toast/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// @flow
import React, { useState } from 'react';
import type { Node } from 'react';
import { I18n } from 'libs/i18n';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faTimes } from '@fortawesome/free-solid-svg-icons';
import css from './Toast.scss';

type Props = {
alert?: string,
notice?: string,
appendDashboardClass?: boolean
};
export type State = {
showToast: boolean,
};

export const Toast = ({
alert, notice, appendDashboardClass,
}: Props): Node => {
const [showAlert, setShowAlert] = useState<boolean>(alert !== null && alert !== '' && !document.documentElement?.hasAttribute('data-turbolinks-preview'));
julianguyen marked this conversation as resolved.
Show resolved Hide resolved
const [showNotice, setShowNotice] = useState<boolean>(notice !== null && notice !== '' && !document.documentElement?.hasAttribute('data-turbolinks-preview'));
const hideNotice = () => {
setShowNotice(false);
};
const hideAlert = () => {
setShowAlert(false);
};
if (showAlert || showNotice) {
setTimeout(() => {
hideNotice();
hideAlert();
}, 7000);
}
return (
<div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use a Fragment here i.e. <> and </>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were using that fragment already but I didn't comprehend your last comment on this change so I converted that to a div, assuming that's what you want :)

<div id="toast-notice" aria-label={showNotice ? I18n.t('alert_auto_hide') : ''} style={{ visibility: showNotice ? 'visible' : 'hidden' }} role="region" aria-live="polite" aria-atomic="true" className={`${showNotice ? 'notice' : ''} ${css.toast} ${showNotice && (showAlert || appendDashboardClass) ? 'smallMarginBottom' : ''}`}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not do inline CSS here and use stylesheets via SCSS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll want to only render this entire element only if showNotice exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we need to have these two divs #toast-notice and #toast-alert present in the DOM all the time because we are using aria-live and role=alert for screen-reader users. So it is a requirement that the element should be present in the page markup first. Please check these links for reference link link-2.

{showNotice && (
<>
<div>
{notice}
</div>
<button type="button" onClick={hideNotice} aria-label={I18n.t('close')}>
<span aria-hidden="true">
<FontAwesomeIcon icon={faTimes} />
</span>
</button>
</>
)}
</div>
<div id="toast-alert" aria-label={showAlert ? I18n.t('alert_auto_hide') : ''} style={{ visibility: showAlert ? 'visible' : 'hidden' }} role="alert" aria-live="assertive" aria-atomic="true" className={`${showAlert ? 'alert' : ''} ${css.toast} ${showAlert && appendDashboardClass ? 'smallMarginBottom' : ''}`}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want to only render this entire element only if showAlert exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we need to have these two divs #toast-notice and #toast-alert present in the DOM all the time because we are using aria-live and role=alert for screen-reader users. So it is a requirement that the element should be present in the page markup first. Please check these links for reference link link-2.

{showAlert && (
<>
<div>
{alert}
</div>
<button type="button" onClick={hideAlert} aria-label={I18n.t('close')}>
<span aria-hidden="true">
<FontAwesomeIcon icon={faTimes} />
</span>
</button>
</>
)}
</div>
</div>
);
};

export default ({
alert, notice, appendDashboardClass,
}: Props): Node => (
<Toast alert={alert} notice={notice} appendDashboardClass={appendDashboardClass} />
);
2 changes: 2 additions & 0 deletions client/app/startup/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Tag } from 'components/Tag';
import { Tooltip } from 'components/Tooltip';
import Input from 'components/Input';
import OAuthButton from 'components/OAuthButton';
import Toast from 'components/Toast';
import Comments from 'widgets/Comments';
import { ToggleLocale } from 'widgets/ToggleLocale';
import Resources from 'widgets/Resources';
Expand Down Expand Up @@ -65,6 +66,7 @@ ReactOnRails.register({
Tag,
ToggleLocale,
Tooltip,
Toast,
CrisisPrevention,
CarePlanContacts,
OAuthButton,
Expand Down
25 changes: 25 additions & 0 deletions client/app/stories/Toast.stories.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable react/jsx-props-no-spreading */
import React from 'react';
import { Toast } from 'components/Toast';

export default {
title: 'Components/Toast',
component: Toast,
};

const Template = (args) => <Toast {...args} />;

export const noticeToast = Template.bind({});

zeeshansarwar38 marked this conversation as resolved.
Show resolved Hide resolved
noticeToast.args = {
notice: 'Login successful.',
appendDashboardClass: 'true',
};
noticeToast.storyName = 'Toast Type: Notice';

export const alertToast = Template.bind({});

alertToast.args = {
alert: 'Login failed.',
};
alertToast.storyName = 'Toast Type: Alert';
4 changes: 3 additions & 1 deletion client/app/styles/_global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ a {
opacity: 0.5;
}
}

.clr-white {
zeeshansarwar38 marked this conversation as resolved.
Show resolved Hide resolved
color: white;
}
@include dashboardSectionStyles();
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ de:
no_text: nein
expand_menu: Menü aufklappen
close: schließen
alert_auto_hide: Deze waarschuwing wordt automatisch verborgen
load_more: Mehr laden
of: von
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ en:
no_text: 'no'
expand_menu: expand menu
close: close
alert_auto_hide: This alert will be hidden automatically
load_more: Load more
of: of
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ es:
no_text: 'no'
expand_menu: ampliar menú
close: cerca
alert_auto_hide: Esta alerta se ocultará automáticamente
load_more: Carga más
of: de
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fr:
no_text: non
expand_menu: dérouler le menu
close: fermer
alert_auto_hide: Cette alerte sera masquée automatiquement
load_more: Charger plus
of: de
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/hi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ hi:
no_text: नहीं
expand_menu: मेनू का विस्तार करें
close: बंद करे
alert_auto_hide: यह अलर्ट अपने आप छिप जाएगा
load_more: और लोड करें
of: का
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/id.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ id:
no_text: Tidak
expand_menu: Perluas menu
close: Tutup
alert_auto_hide: Lansiran ini akan disembunyikan secara otomatis
load_more: Muat lebih banyak
of: dari
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ it:
no_text: 'no'
expand_menu: espandere il menu
close: vicino
alert_auto_hide: Questo avviso verrà nascosto automaticamente
load_more: Carica altro
of: di
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/ko.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ko:
no_text: 아니오
expand_menu: 추가 메뉴
close: 종료
alert_auto_hide: 이 알림은 자동으로 숨겨집니다.
load_more: 더 보기
of: of
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/nb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ nb:
no_text: nei
expand_menu: utvide menyen
close: lukk
alert_auto_hide: Dette varselet vil bli skjult automatisk
load_more: Last mer
of: av
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ nl:
no_text: nee
expand_menu: vouw menu uit
close: sluiten
alert_auto_hide: Deze waarschuwing wordt automatisch verborgen
load_more: Meer laden
of: van
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pt-BR:
no_text: não
expand_menu: expandir menu
close: fechar
alert_auto_hide: Este alerta será ocultado automaticamente
load_more: Carregue mais
of: do
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/sv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ sv:
no_text: nej
expand_menu: expandera menyn
close: stänga
alert_auto_hide: Denna varning kommer att döljas automatiskt
load_more: Ladda mer
of: av
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/vi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ vi:
no_text: không
expand_menu: mở rộng menu
close: gần
alert_auto_hide: Cảnh báo này sẽ tự động bị ẩn
load_more: Tải thêm
of: của
languages:
Expand Down
1 change: 1 addition & 0 deletions config/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ zh-CN:
no_text: 没有
expand_menu: 展开菜单
close: 关闭
alert_auto_hide: 此警报将自动隐藏
load_more: 更多
of: 的
languages:
Expand Down