|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2015 - present Instructure, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +/** @jsx jsx */ |
| 26 | +import { Component } from 'react' |
| 27 | + |
| 28 | +import { withStyle, jsx } from '@instructure/emotion' |
| 29 | +import { Table } from '@instructure/ui-table' |
| 30 | +import { View } from '@instructure/ui-view' |
| 31 | + |
| 32 | +import { ColorSwatch } from '../ColorSwatch' |
| 33 | + |
| 34 | +import generateStyle from './styles' |
| 35 | +import { propTypes, allowedProps } from './props' |
| 36 | +import type { ComponentThemeProps } from './props' |
| 37 | + |
| 38 | +@withStyle(generateStyle, null) |
| 39 | +class ComponentTheme extends Component<ComponentThemeProps> { |
| 40 | + static propTypes = propTypes |
| 41 | + static allowedProps = allowedProps |
| 42 | + |
| 43 | + renderValueCell( |
| 44 | + value: undefined | string | object | number, |
| 45 | + colorPrimitives: object |
| 46 | + ) { |
| 47 | + if (!value) { |
| 48 | + return <code>$aposundefined$apos</code> |
| 49 | + } |
| 50 | + if (typeof value === 'object') { |
| 51 | + return <code>{JSON.stringify(value)}</code> |
| 52 | + } |
| 53 | + if (typeof value === 'object') { |
| 54 | + return <code>{JSON.stringify(value)}</code> |
| 55 | + } |
| 56 | + if ( |
| 57 | + value.toString().charAt(0) === '#' || |
| 58 | + value.toString().substring(0, 3) === 'rgb' |
| 59 | + ) { |
| 60 | + // find color primitive name from hex value |
| 61 | + const color = Object.entries(colorPrimitives).find(([, v]) => v === value) |
| 62 | + return ( |
| 63 | + <span> |
| 64 | + <View margin="0 xx-small 0 0"> |
| 65 | + <ColorSwatch color={value} /> |
| 66 | + </View> |
| 67 | + <code>{color?.[0] ?? value}</code> |
| 68 | + </span> |
| 69 | + ) |
| 70 | + } |
| 71 | + return <code>{value}</code> |
| 72 | + } |
| 73 | + |
| 74 | + renderRows() { |
| 75 | + const { componentTheme, themeVariables } = this.props |
| 76 | + const colorPrimitives = themeVariables.colors.primitives |
| 77 | + |
| 78 | + return Object.keys(componentTheme).map((name) => { |
| 79 | + return ( |
| 80 | + <Table.Row key={name}> |
| 81 | + <Table.Cell> |
| 82 | + <code>{name}</code> |
| 83 | + </Table.Cell> |
| 84 | + <Table.Cell> |
| 85 | + {this.renderValueCell(componentTheme[name], colorPrimitives)} |
| 86 | + </Table.Cell> |
| 87 | + </Table.Row> |
| 88 | + ) |
| 89 | + }) |
| 90 | + } |
| 91 | + |
| 92 | + render() { |
| 93 | + const { componentTheme, styles } = this.props |
| 94 | + |
| 95 | + return componentTheme && Object.keys(componentTheme).length > 0 ? ( |
| 96 | + <div css={styles?.componentTheme}> |
| 97 | + <Table caption="Component theme"> |
| 98 | + <Table.Head> |
| 99 | + <Table.Row> |
| 100 | + <Table.ColHeader id="name">Name</Table.ColHeader> |
| 101 | + <Table.ColHeader id="value">Value</Table.ColHeader> |
| 102 | + </Table.Row> |
| 103 | + </Table.Head> |
| 104 | + <Table.Body>{this.renderRows()}</Table.Body> |
| 105 | + </Table> |
| 106 | + </div> |
| 107 | + ) : null |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +export default ComponentTheme |
| 112 | +export { ComponentTheme } |
0 commit comments