Библиотека компонентов "SDDS-CS"
Реализация компонентов для создания веб-приложений.
Использование
Компоненты реализованы на typescript с помощью react , styled-components и emotion;
Использование данного пакета предполагает установку зависимостей: react
& react-dom
;
Использование styled-components
и emotion
на проект необязательно, так же как и использование typescript
.
Но для того чтобы компоненты работали корректно необходимо установить styled-components
или emotion
.
Установка зависимостей
$ npm install --save react react-dom
$ npm install --save @salutejs/sdds-cs @salutejs/sdds-themes
Так же надо установить пакет styled-components. Примечание: Важно использовать версию пакета 5.3.1
$ npm install --save styled-components@5.3.1
Или, если вы используете пакет @emotion, то
$ npm install --save @emotion/styled @emotion/react @emotion/css
Подключение тем
Типографическая система основана на фирменных шрифтах. Для того чтобы шрифт было удобно поставлять в web-приложения, шрифт был загружен на CDN.
Для использования типографическ ой системы необходимо загрузить два css
файла в зависимости от используемых шрифтов в теме.
Их необходимо добавить внутрь тега head
. Если в качестве основы web-приложения вы используете create-react-app, вам необходимо изменить файл ./public/index.html
.
<html>
<head>
<link rel="stylesheet" href="https://cdn-app.sberdevices.ru/shared-static/0.0.0/styles/SBSansText.0.2.0.css" />
<link
rel="stylesheet"
href="https://cdn-app.sberdevices.ru/shared-static/0.0.0/styles/SBSansDisplay.0.2.0.css"
/>
</head>
<body>
...
</body>
</html>
С помощью styled-component
import React from 'react';
import { createGlobalStyle } from 'styled-components';
import { Button, BodyL } from '@salutejs/sdds-cs';
import { sdds_cs__light } from '@salutejs/sdds-themes';
const Theme = createGlobalStyle(sdds_cs__light);
const App = () => {
return (
<>
<Theme />
<BodyL>Hello world</BodyL>
<Button text="This is themed button" />
</>
);
};
export default App;
С помощью emotion
import React from 'react';
import { Global, css } from '@emotion/react';
import { Button, BodyL } from '@salutejs/sdds-cs';
import { sdds_cs__light } from '@salutejs/sdds-themes';
const themeStyle = css(sdds_cs__light);
const App = () => {
return (
<>
<Global styles={themeStyle} />
<BodyL>Hello world</BodyL>
<Button text="This is themed button" />
</>
);
};
export default App;
С помощью импорта css
файла
import React from 'react';
import { Button, BodyL } from '@salutejs/sdds-cs';
import '@salutejs/sdds-themes/css/sdds_cs__dark.css';
const App = () => {
return (
<>
<BodyL>Hello world</BodyL>
<Button text="This is themed button" />
</>
);
};
export default App;
С помощью импорта модульного css
файла
Потребуется дополнительно настроить TypeScript
import React from 'react';
import { Button, BodyL } from '@salutejs/sdds-cs';
import styles from '@salutejs/sdds-themes/css/sdds_cs.module.css';
const App = () => {
return (
<div className={styles.dark}>
<BodyL>Hello world</BodyL>
<Button text="This is themed button" />
</div>
);
};
export default App;
Смена тем
Пример на styled-components
:
import React, { useState } from 'react';
import { createGlobalStyle } from 'styled-components';
import { Switch } from '@salutejs/sdds-cs';
import { sdds_cs__light, sdds_cs__dark } from '@salutejs/sdds-themes';
import './style.css';
const LightTheme = createGlobalStyle(sdds_cs__light);
const DarkTheme = createGlobalStyle(sdds_cs__dark);
const App = () => {
const [theme, setTheme] = useState('light');
return (
<>
{theme === 'light' ? <LightTheme /> : <DarkTheme />}
<div className="wrapper">
<Switch
label="dark theme: "
onChange={() => {
setTheme((theme) => (theme === 'light' ? 'dark' : 'light'));
}}
/>
</div>
</>
);
};
export default App;
Пример на emotion
:
import React, { useState } from 'react';
import { Global, css } from '@emotion/react';
import { Switch } from '@salutejs/sdds-cs';
import { sdds_cs__light, sdds_cs__dark } from '@salutejs/sdds-themes';
import './style.css';
const lightThemeStyle = css(sdds_cs__light);
const darkThemeStyle = css(sdds_cs__dark);
const App = () => {
const [theme, setTheme] = useState('light');
return (
<>
<Global styles={theme === 'light' ? lightThemeStyle : darkThemeStyle} />
<div className="wrapper">
<Switch
label="dark theme: "
onChange={() => setTheme((theme) => (theme === 'light' ? 'dark' : 'light'))}
/>
</div>
</>
);
};
export default App;
Пример на css-modules
:
import React, { useLayoutEffect, useState } from 'react';
import { Switch } from '@salutejs/sdds-cs';
import webThemes from '@salutejs/sdds-themes/css/sdds_cs.module.css';
import './style.css';
const App = () => {
const [theme, setTheme] = useState('light');
useLayoutEffect(() => {
document.documentElement.className = webThemes[theme];
}, [theme]);
return (
<div className="wrapper">
<Switch
label="dark theme: "
onChange={() => {
setTheme((theme) => (theme === 'light' ? 'dark' : 'light'));
}}
/>
</div>
);
};
export default App;
Пример на css
:
import React, { useLayoutEffect, useState } from 'react';
import { Switch } from '@salutejs/sdds-cs';
// добавьте импорт в css файл
// @import '@salutejs/sdds-themes/css/sdds_cs.module.css';
import './style.css';
const App = () => {
const [theme, setTheme] = useState('light');
useLayoutEffect(() => {
document.documentElement.className = theme;
}, [theme]);
return (
<div className="wrapper">
<Switch
label="dark theme: "
onChange={() => {
setTheme((theme) => (theme === 'light' ? 'dark' : 'light'));
}}
/>
</div>
);
};
export default App;
Корень приложения
В корне приложения вызовите компонент глобальных стилей GlobalStyle
:
- Если вы используете Create React App, делайте вызов внутри
src/index.tsx
. - Если вы используете Next.js, создайте файл
pages/_app.tsx
и подключите стили в нем.
Для корректной работы SSR - server side rendering
приложение нужно обернуть SSRProvider
;
Использование токенов
Все css
токены завернуты в js
переменные для более удобного доступа:
/** Основной цвет текста */
export const textPrimary = 'var(--text-primary, #F5F5F5)';
/** Основной фон */
export const backgroundPrimary = 'var(--background-primary, #000000)';
Так же в пакете есть типографические токены, для случаев, когда необходимо точечно применить типографику к контейнеру.
/** Токены типографики для компонента DsplL */
export const dsplL = ({
fontFamily: 'var(--plasma-typo-dspl-l-font-family)',
fontSize: 'var(--plasma-typo-dspl-l-font-size)',
fontStyle: 'var(--plasma-typo-dspl-l-font-style)',
fontWeight: 'var(--plasma-typo-dspl-l-font-weight)',
letterSpacing: 'var(--plasma-typo-dspl-l-letter-spacing)',
lineHeight: 'var(--plasma-typo-dspl-l-line-height)',
} as unknown) as CSSObject;
Есть два пути импорта токенов:
- Из вертикали
@salutejs/sdds-themes/tokens
- подходит в большинстве случаев, т.к. там лежит весь базовый набор токенов. - Непосредственно из темы
@salutejs/sdds-themes/tokens/sdds_cs
- следует использовать, когда необходимо импортировать уникальные токены, которые лежат только в этой теме.
Пример использования:
import React from 'react';
import styled from 'styled-components';
import { textAccent, backgroundPrimary, textL } from '@salutejs/sdds-themes/tokens';
const AppStyled = styled.div`
padding: 2rem;
color: ${textAccent};
background-color: ${backgroundPrimary};
`;
const Container = styled.div`
${textL};
margin: 1rem;
`;
const App = () => {
return (
<AppStyled>
<Container>
<span>Hello world</span>
</Container>
</AppStyled>
);
};
export default App;
Использование компонентов
Все компоненты доступны из папки components
или напрямую из пакета:
import styled from 'styled-components';
import { Button } from '@salutejs/sdds-cs';
import { textAccent } from '@salutejs/sdds-themes/tokens';
export const App = () => {
const StyledP = styled.p`
color: ${textAccent};
`;
return (
<>
<Button>Hello, Plasma!</Button>
<StyledP>
Token usage example
</StyledP>
</>
);
};