color.stories.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. import styled from '@emotion/styled';
  2. import DoAccentColors from 'sentry-images/stories/color/do-accent-colors.svg';
  3. import DoContrast from 'sentry-images/stories/color/do-contrast.svg';
  4. import DoDifferentiation from 'sentry-images/stories/color/do-differentiation.svg';
  5. import DontAccentColors from 'sentry-images/stories/color/dont-accent-colors.svg';
  6. import DontContrast from 'sentry-images/stories/color/dont-contrast.svg';
  7. import DontDifferentiation from 'sentry-images/stories/color/dont-differentiation.svg';
  8. import ExternalLink from 'sentry/components/links/externalLink';
  9. import Panel from 'sentry/components/panels/panel';
  10. import PanelItem from 'sentry/components/panels/panelItem';
  11. import ThemeToggle from 'sentry/components/stories/themeToggle';
  12. import {IconCheckmark, IconClose} from 'sentry/icons';
  13. import {space} from 'sentry/styles/space';
  14. import theme, {ColorOrAlias} from 'sentry/utils/theme';
  15. interface Palette {
  16. color: ColorOrAlias;
  17. text: ColorOrAlias;
  18. }
  19. const GRAY_PALETTES: Palette[][] = [
  20. [{color: 'gray500', text: 'lightModeWhite'}],
  21. [{color: 'gray400', text: 'lightModeWhite'}],
  22. [{color: 'gray300', text: 'lightModeWhite'}],
  23. [{color: 'gray200', text: 'lightModeBlack'}],
  24. [{color: 'gray100', text: 'lightModeBlack'}],
  25. ];
  26. const LEVELS_PALETTES: Palette[][] = [
  27. [
  28. {color: 'purple400', text: 'lightModeWhite'},
  29. {color: 'purple300', text: 'lightModeWhite'},
  30. {color: 'purple200', text: 'lightModeBlack'},
  31. {color: 'purple100', text: 'lightModeBlack'},
  32. ],
  33. [
  34. {color: 'blue400', text: 'lightModeWhite'},
  35. {color: 'blue300', text: 'lightModeWhite'},
  36. {color: 'blue200', text: 'lightModeBlack'},
  37. {color: 'blue100', text: 'lightModeBlack'},
  38. ],
  39. [
  40. {color: 'green400', text: 'lightModeWhite'},
  41. {color: 'green300', text: 'lightModeBlack'},
  42. {color: 'green200', text: 'lightModeBlack'},
  43. {color: 'green100', text: 'lightModeBlack'},
  44. ],
  45. [
  46. {color: 'yellow400', text: 'lightModeBlack'},
  47. {color: 'yellow300', text: 'lightModeBlack'},
  48. {color: 'yellow200', text: 'lightModeBlack'},
  49. {color: 'yellow100', text: 'lightModeBlack'},
  50. ],
  51. [
  52. {color: 'red400', text: 'lightModeWhite'},
  53. {color: 'red300', text: 'lightModeWhite'},
  54. {color: 'red200', text: 'lightModeBlack'},
  55. {color: 'red100', text: 'lightModeBlack'},
  56. ],
  57. [
  58. {color: 'pink400', text: 'lightModeWhite'},
  59. {color: 'pink300', text: 'lightModeWhite'},
  60. {color: 'pink200', text: 'lightModeBlack'},
  61. {color: 'pink100', text: 'lightModeBlack'},
  62. ],
  63. ];
  64. const FixedWidth = styled('div')`
  65. max-width: 800px;
  66. `;
  67. export default function ColorStories() {
  68. return (
  69. <FixedWidth>
  70. <h3>Colors</h3>
  71. <p>
  72. Sentry has a flexible, tiered color system that adapts to both light and dark
  73. mode. Our color palette consists of neutral grays and 6 accent colors.
  74. </p>
  75. <hr />
  76. <h4>Grays</h4>
  77. <p>
  78. There are 5 shades of gray, ranging from Gray 500 (darkest) to Gray 100
  79. (lightest).
  80. </p>
  81. <p>
  82. <strong>Gray 300 and above</strong> are accessible foreground colors that conform
  83. to{' '}
  84. <ExternalLink href="https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum.html">
  85. WCAG standards
  86. </ExternalLink>
  87. . Use them as text and icon colors.
  88. </p>
  89. <p>Here are the recommended use cases:</p>
  90. <ul>
  91. <li>
  92. <strong>Gray 500:</strong> headings, button labels, tags/badges, and alerts.
  93. </li>
  94. <li>
  95. <strong>Gray 400:</strong> body text, input values & labels.
  96. </li>
  97. <li>
  98. <strong>Gray 300:</strong> input placeholders, inactive/disabled inputs and
  99. buttons, chart labels, supplemental and non-essential text
  100. </li>
  101. <li>
  102. <strong>Gray 200:</strong> borders around large elements (cards, panels,
  103. dialogs, tables).
  104. </li>
  105. <li>
  106. <strong>Gray 100:</strong> dividers and borders around small elements (buttons,
  107. form inputs).
  108. </li>
  109. </ul>
  110. <ThemeToggle>
  111. <ColorPalette name="grays" palette={GRAY_PALETTES} />
  112. </ThemeToggle>
  113. <hr />
  114. <h4>Accent Colors</h4>
  115. <p>
  116. Accent colors help shift the user's focus to certain interactive and high-priority
  117. elements, like links, buttons, and warning banners.
  118. </p>
  119. <h5>Hues</h5>
  120. <p>There are 6 hues to choose from. Each has specific connotations:</p>
  121. <ul>
  122. <li>
  123. <strong>Purple:</strong> brand, current/active/focus state, or new information.
  124. </li>
  125. <li>
  126. <strong>Blue:</strong> hyperlink.
  127. </li>
  128. <li>
  129. <strong>Green:</strong> success, resolution, approval, availability, or
  130. creation.
  131. </li>
  132. <li>
  133. <strong>Yellow:</strong> warning, missing, or impeded progress.
  134. </li>
  135. <li>
  136. <strong>Red:</strong> fatal error, deletion, or removal.
  137. </li>
  138. <li>
  139. <strong>Pink:</strong> new feature or promotion.
  140. </li>
  141. </ul>
  142. <h5>Levels</h5>
  143. <p>
  144. Each hue comes in 4 levels: 400 (dark), 300 (full opacity), 200 (medium opacity),
  145. and 100 (low opacity).
  146. </p>
  147. <ul>
  148. <li>
  149. <strong>The 400 level</strong> is a darkened version of 300. It is useful for
  150. hover/active states in already accentuated elements. For example, a button could
  151. have a background of Purple 300 in normal state and Purple 400 on hover.
  152. </li>
  153. <li>
  154. <strong>The 300 level</strong> has full opacity and serves well as text and icon
  155. colors (with the exception of Yellow 300, which does not meet{' '}
  156. <ExternalLink href="https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum.html">
  157. WCAG's contrast standards
  158. </ExternalLink>
  159. ).
  160. </li>
  161. <li>
  162. <strong>The 200 level</strong> has medium opacity, useful for borders and
  163. dividers.
  164. </li>
  165. <li>
  166. <strong>The 100 level</strong> has very low opacity, useful as background fills.
  167. </li>
  168. </ul>
  169. <ThemeToggle>
  170. <ColorPalette name="levels" palette={LEVELS_PALETTES} />
  171. </ThemeToggle>
  172. <hr />
  173. <h4>Accessibility</h4>
  174. <p>
  175. When it comes to using color, there are two main accessibility concerns:
  176. readability and separation.
  177. </p>
  178. <h5>Readability</h5>
  179. <p>
  180. <ExternalLink href="https://www.w3.org/TR/WCAG21/">WCAG</ExternalLink> requires
  181. that normal text elements have a contrast ratio of at least 4.5:1 against the
  182. background. For large text (at least 16px in size AND in medium/bold weight), the
  183. required ratio is lower, at 3:1. This is to ensure a comfortable reading
  184. experience in different lighting conditions.{' '}
  185. <ExternalLink href="https://webaim.org/resources/contrastchecker/">
  186. Use this tool
  187. </ExternalLink>{' '}
  188. to confirm text contrast ratios.
  189. </p>
  190. <p>
  191. In Sentry's color palette, only Gray 300 and above satisfy the contrast
  192. requirement for normal text. This applies to both light and dark mode.
  193. </p>
  194. <p>
  195. Accent colors in the 300 series, except for Yellow 300, satisfy the contrast
  196. requirement for large text.
  197. </p>
  198. <SideBySideList>
  199. <ExampleCard
  200. imgSrc={DoContrast}
  201. text="Use Gray 300 and above for normal text"
  202. isPositive
  203. />
  204. <ExampleCard
  205. imgSrc={DontContrast}
  206. text="Use Gray 100 or 200 for normal text, as they don't have the required the contrast levels"
  207. />
  208. <ExampleCard
  209. imgSrc={DoAccentColors}
  210. text="Use accent colors in the 300 series (except for Yellow 300) for large text, if needed"
  211. isPositive
  212. />
  213. <ExampleCard
  214. imgSrc={DontAccentColors}
  215. text="Use accent colors in the 100 or 200 series for any text"
  216. />
  217. </SideBySideList>
  218. <h5>Separation</h5>
  219. <p>
  220. Color can be an effective way to visually separate elements in the user interface.
  221. However, not all users see color in the same way. Some are color-blind and cannot
  222. reliably differentiate one color from another. Some have color filters on their
  223. screens, like Night Shift in MacOS. Others are in bright environments with high
  224. levels of glare, reducing their ability to see color clearly.
  225. </p>
  226. <p>
  227. As such, color is an unreliable way to separate elements. Whenever possible,
  228. provide additional visual cues like icons, text labels, line type (solid, dashed,
  229. dotted),… to further reinforce the separation.
  230. </p>
  231. <SideBySideList>
  232. <ExampleCard
  233. imgSrc={DoDifferentiation}
  234. text="Provide additional visual encoding (e.g. line type) besides color to differentiate elements"
  235. isPositive
  236. />
  237. <ExampleCard
  238. imgSrc={DontDifferentiation}
  239. text="Use color as the only way to differentiate elements"
  240. />
  241. </SideBySideList>
  242. </FixedWidth>
  243. );
  244. }
  245. const SideBySideList = styled('ul')`
  246. /* Reset */
  247. list-style-type: none;
  248. margin: 0;
  249. padding: 0;
  250. & > li {
  251. margin: 0;
  252. }
  253. & > li > div {
  254. margin-bottom: 0;
  255. }
  256. /* Side-by-side display */
  257. display: grid;
  258. grid-template-columns: 1fr 1fr;
  259. gap: ${space(2)};
  260. `;
  261. const PalettePanel = styled(Panel)`
  262. margin-bottom: 0;
  263. `;
  264. const PalettePanelItem = styled(PanelItem)<{
  265. color: ColorOrAlias;
  266. text: ColorOrAlias;
  267. }>`
  268. flex-direction: column;
  269. gap: ${space(0.5)};
  270. &:first-child {
  271. border-radius: ${p => p.theme.borderRadiusTop};
  272. }
  273. &:last-child {
  274. border-radius: ${p => p.theme.borderRadiusBottom};
  275. }
  276. &:first-child:last-child {
  277. border-radius: ${p => p.theme.borderRadius};
  278. }
  279. background: ${p => p.theme[p.color]};
  280. color: ${p => p.theme[p.text]};
  281. `;
  282. function ColorPalette({name, palette}: {name: string; palette: Palette[][]}) {
  283. return (
  284. <SideBySideList>
  285. {palette.map((section, i) => {
  286. return (
  287. <li key={`${name}-${i}`}>
  288. <PalettePanel typeof="ul">
  289. {section.map(color => {
  290. return (
  291. <PalettePanelItem
  292. key={`${name}-${color.color}`}
  293. color={color.color}
  294. text={color.text}
  295. >
  296. <strong>{color.color}</strong>
  297. {theme[color.color]}
  298. </PalettePanelItem>
  299. );
  300. })}
  301. </PalettePanel>
  302. </li>
  303. );
  304. })}
  305. </SideBySideList>
  306. );
  307. }
  308. const ExampleImg = styled('img')`
  309. border: 1px solid ${p => p.theme.border};
  310. border-radius: ${p => p.theme.borderRadius};
  311. max-width: 400px;
  312. `;
  313. const PositiveLabel = styled(({className}: {className?: string}) => (
  314. <div className={className}>
  315. <IconCheckmark />
  316. DO
  317. </div>
  318. ))`
  319. color: ${p => p.theme.green400};
  320. align-items: center;
  321. display: flex;
  322. font-weight: bold;
  323. gap: ${space(0.5)};
  324. `;
  325. const NegativeLabel = styled(({className}: {className?: string}) => (
  326. <div className={className}>
  327. <IconClose color="red400" />
  328. DON'T
  329. </div>
  330. ))`
  331. color: ${p => p.theme.red400};
  332. align-items: center;
  333. display: flex;
  334. font-weight: bold;
  335. gap: ${space(0.5)};
  336. `;
  337. const ExampleCardGrid = styled('figcaption')`
  338. display: grid;
  339. grid-template-columns: 1fr 2fr;
  340. align-items: flex-start;
  341. color: ${p => p.theme.subText};
  342. padding: ${space(1)} ${space(1)} 0;
  343. `;
  344. interface ExampleCardProps {
  345. imgSrc: string;
  346. text: string;
  347. isPositive?: boolean;
  348. }
  349. function ExampleCard({imgSrc, text, isPositive}: ExampleCardProps) {
  350. return (
  351. <figure>
  352. <ExampleImg src={imgSrc} />
  353. <ExampleCardGrid>
  354. {isPositive ? <PositiveLabel /> : <NegativeLabel />}
  355. <span>{text}</span>
  356. </ExampleCardGrid>
  357. </figure>
  358. );
  359. }