2424
2525 */
2626
27- import React , { useContext } from 'react'
28- import styled , { ThemeContext } from 'styled-components'
27+ import React , { Fragment , ReactNode } from 'react'
28+ import styled from 'styled-components'
2929import sortBy from 'lodash/sortBy'
30+
3031/**
3132 * IMPORTANT: It is only acceptable to reach across package boundaries like this
3233 * because this is a Story and will never be run in a "production" environment.
3334 *
3435 * In this case we don't want to create a dependency loop between @looker/design-tokens
3536 * and @looker/components
3637 */
37- import { Code , Flex , Heading } from '../../components/src'
38+ import { Tooltip } from '../../components/src'
39+ import { theme } from './theme'
40+
41+ const CELL_SIZE = '3rem'
42+
43+ const sortedColorsArray = sortBy ( Object . entries ( theme . colors ) , 0 )
3844
39- const CELL_SIZE = '6rem'
45+ export const AllColors = ( ) => {
46+ const colors = sortedColorsArray . map ( ( [ name , color ] ) => {
47+ return { color, label : `${ name } - ${ color } ` }
48+ } )
4049
41- export const Complete = ( ) => (
42- < GridLayout >
43- < ColorSlots />
44- </ GridLayout >
45- )
50+ return < MiniSwatches > { colors } </ MiniSwatches >
51+ }
52+
53+ export const GroupedByOutput = ( ) => {
54+ const grouped : { [ key : string ] : string [ ] } = { }
55+
56+ sortedColorsArray . forEach ( ( [ name , color ] ) => {
57+ if ( grouped [ color ] ) {
58+ grouped [ color ] = [ ...grouped [ color ] , name ]
59+ } else {
60+ grouped [ String ( color ) ] = [ name ]
61+ }
62+ } )
63+
64+ const colors = Object . entries ( grouped ) . map ( ( [ color , entries ] ) => {
65+ const labels = entries . map ( ( label , index ) => (
66+ < Fragment key = { index } >
67+ { label } < br />
68+ </ Fragment >
69+ ) )
70+
71+ return {
72+ color,
73+ label : (
74+ < >
75+ < strong > { color } </ strong >
76+ < br />
77+ { labels }
78+ </ >
79+ ) ,
80+ }
81+ } )
82+
83+ return < MiniSwatches > { colors } </ MiniSwatches >
84+ }
85+
86+ GroupedByOutput . parameters = {
87+ storyshots : { disable : true } ,
88+ }
4689
4790export default {
48- component : Complete ,
4991 title : 'Colors' ,
5092}
5193
5294const GridLayout = styled . div `
5395 display: grid;
5496 grid-gap: 1rem;
5597 grid-template-columns: repeat(auto-fill, minmax(${ CELL_SIZE } , 1fr));
98+ padding: 1rem;
5699`
57100
58101/**
@@ -74,31 +117,16 @@ const CircleSwatch = styled.div<SwatchProps>`
74117 width: 3rem;
75118`
76119
77- const ColorSlots = ( ) => {
78- const { colors } = useContext ( ThemeContext )
79-
80- const colorSwatches = sortBy ( Object . entries ( colors ) , 0 ) . map (
81- ( [ name , color ] ) => (
82- < Flex
83- flexDirection = "column"
84- key = { name }
85- alignItems = "center"
86- width = { CELL_SIZE }
87- >
88- < CircleSwatch color = { color } > </ CircleSwatch >
89- < Heading
90- truncate
91- fontSize = "small"
92- pt = "xsmall"
93- fontWeight = "semiBold"
94- fontFamily = "body"
95- >
96- { name }
97- </ Heading >
98- < Code fontSize = "xsmall" > { color . toUpperCase ( ) } </ Code >
99- </ Flex >
100- )
101- )
102-
103- return < > { colorSwatches } </ >
120+ interface MiniSwatchesProps {
121+ children : { color : string ; label : ReactNode } [ ]
122+ }
123+
124+ const MiniSwatches = ( { children } : MiniSwatchesProps ) => {
125+ const colorSwatches = children . map ( ( { label, color } , index ) => (
126+ < Tooltip key = { index } content = { label } textAlign = "left" >
127+ < CircleSwatch color = { color } > </ CircleSwatch >
128+ </ Tooltip >
129+ ) )
130+
131+ return < GridLayout > { colorSwatches } </ GridLayout >
104132}
0 commit comments