Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | // Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license
import React, { Component } from 'react';
import classNames from 'classnames';
import Copy from '../../shared/components/Copy';
import { propTypes } from '../../scripts/var-metadata';
const getPropTypeData = (name, varName) => {
const trimmedVar = varName.replace(`--slds-c-${name}-`, '');
let propType = null;
let valueTypes = null;
Object.keys(propTypes).forEach((prop) => {
if (trimmedVar.match(prop)) {
propType = propTypes[prop].type;
valueTypes = propTypes[prop].valueTypes;
}
});
return {
propType,
valueTypes,
};
};
const formatType = (type) => {
let formattedType = type.toLowerCase();
if (formattedType === 'color') {
formattedType += '_value'; // MDN adds _value to color data type page, this discrepancy is unique
}
return formattedType;
};
class StylingHooksTable extends Component {
state = {
vars: null,
};
async componentDidMount() {
try {
const { name, type } = this.props;
const response = await import(`../../.generated/metadata/${type}s/${name}/styling-hooks.json`);
const vars = response.default;
this.setState({ vars });
} catch (error) {
console.error(error);
}
}
render() {
const { name } = this.props;
const { vars } = this.state;
const categories =
vars &&
Object.keys(vars)
.map((varName) => {
return getPropTypeData(name, varName).propType;
})
.filter((value, i, self) => self.indexOf(value) === i)
.sort();
return vars ? (
<div className="site">
<div className="slds-m-bottom_large">
<p className="doc">
<strong>
The component styling hooks in this table are compatible with Lightning UI, but they aren’t
compatible with Enhanced Lightning UI.
</strong>
</p>
<p className="doc">
Use these CSS Custom Properties as hooks to customize this SLDS component with your own style. For
more information,{' '}
<a href="/platforms/lightning/styling-hooks/">read the technical documentation</a>.
</p>
</div>
<div className="slds-scrollable--x">
<table className="slds-table hooks-table slds-no-row-hover">
<thead>
<tr>
<th>Category</th>
<th>Styling Hook Name</th>
<th>Value Type(s)</th>
<th>Fallback Value</th>
</tr>
</thead>
<tbody>
{categories.map((category, i) => {
const categoryVars = Object.keys(vars)
.filter((varName) => getPropTypeData(name, varName).propType === category)
.map((varName) => {
return {
name: varName,
value: vars[varName].fallbackValue,
types: getPropTypeData(name, varName).valueTypes || [],
};
})
.sort((a, b) => {
const nameA = a.name;
const nameB = b.name;
if (nameA < nameB) {
return -1; // nameA comes first
}
if (nameA > nameB) {
return 1; // nameB comes first
}
return 0; // names must be equal
});
return (
<>
{categoryVars.map((varData, ii) => (
<tr
key={`${category}-${i}-${ii}`}
className={classNames({
'hooks-table__section': ii === 0,
'hooks-table__section_end': ii === categoryVars.length - 1,
})}
>
{ii === 0 && (
<th
className="hooks-table__col-category"
scope="rowgroup"
rowSpan={categoryVars.length}
>
{category}
</th>
)}
<td>
<div className="slds-grid">
<Copy
key={`copy${varData.name}`}
containerClassName="hooks-table__button-clipboard-copy"
className="slds-button_icon-border-filled slds-button_icon-x-small slds-m-right_small"
text={varData.name}
/>
<code className="hooks-table__hook-name">{varData.name}</code>
</div>
</td>
<td>
{varData.types.map((type, x, arr) => {
const formattedType = formatType(type);
return (
<>
<a
key={`${category}-${i}-${ii}-${x}`}
href={`https://developer.mozilla.org/en-US/docs/Web/CSS/${formattedType}`}
>
{type}
</a>
{/* Proper comma separation when there is more than 1 valueType */}
{arr.length > 1 && x <= 0 && ', '}
</>
);
})}
</td>
<td>{varData.value}</td>
</tr>
))}
</>
);
})}
</tbody>
</table>
</div>
</div>
) : (
<></>
);
}
}
export default StylingHooksTable;
|