Skip to content

Commit f8b040d

Browse files
authored
Merge pull request #233 from dvtng/srmagura/highlight-width
Custom highlight background
2 parents d8c1492 + c097345 commit f8b040d

9 files changed

+73
-7
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 3.5.0
2+
3+
### Features
4+
5+
- Add optional `customHighlightBackground` prop. (#233)
6+
17
## 3.4.0
28

39
### Features

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,13 @@ return (
235235
</td>
236236
<td><code>true</code></td>
237237
</tr>
238+
<tr>
239+
<td><code>customHighlightBackground?: string</code></td>
240+
<td>
241+
Allows you to override the <code>background-image</code> property of the highlight element, enabling you to fully customize the gradient. See example below.
242+
</td>
243+
<td><code>undefined</code></td>
244+
</tr>
238245
</tbody>
239246
</table>
240247

@@ -273,6 +280,18 @@ const wrapped2 = (
273280
);
274281
```
275282

283+
### Custom Highlight Background
284+
285+
You may want to make the gradient used in the highlight element narrower or wider. To do this, you can set the `customHighlightBackground` prop. Here's an example of a narrow highlight:
286+
287+
```tsx
288+
<Skeleton customHighlightBackground="linear-gradient(90deg, var(--base-color) 40%, var(--highlight-color) 50%, var(--base-color) 60%)" />
289+
```
290+
291+
**If you use this prop, the `baseColor` and `highlightColor` props are ignored,** but you can still reference their corresponding CSS variables as shown in the above example.
292+
293+
![Custom highlight background example](assets/custom-highlight-background.png)
294+
276295
## Troubleshooting
277296

278297
### The skeleton width is 0 when the parent has `display: flex`!
17.2 KB
Loading

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-loading-skeleton",
3-
"version": "3.4.0",
3+
"version": "3.5.0",
44
"description": "Make beautiful, animated loading skeletons that automatically adapt to your app.",
55
"keywords": [
66
"react",

src/Skeleton.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ function styleOptionsToCssProperties({
1818
direction,
1919
duration,
2020
enableAnimation = defaultEnableAnimation,
21+
22+
customHighlightBackground,
2123
}: SkeletonStyleProps & { circle: boolean }): CSSProperties &
2224
Record<`--${string}`, string> {
2325
const style: ReturnType<typeof styleOptionsToCssProperties> = {};
@@ -41,6 +43,9 @@ function styleOptionsToCssProperties({
4143
if (typeof highlightColor !== 'undefined')
4244
style['--highlight-color'] = highlightColor;
4345

46+
if (typeof customHighlightBackground === 'string')
47+
style['--custom-highlight-background'] = customHighlightBackground;
48+
4449
return style;
4550
}
4651

src/SkeletonStyleProps.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ export interface SkeletonStyleProps {
1010
duration?: number;
1111
direction?: 'ltr' | 'rtl';
1212
enableAnimation?: boolean;
13+
14+
customHighlightBackground?: string;
1315
}

src/__stories__/Skeleton.stories.tsx

+31
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,34 @@ export const PrefersReducedMotion = () => (
427427
/>
428428
</div>
429429
);
430+
431+
export const HighlightWidth = () => (
432+
<div style={{ width: 500 }}>
433+
<p>Default</p>
434+
<Skeleton
435+
highlightColor="#E0B0FF"
436+
customHighlightBackground="linear-gradient(90deg, var(--base-color) 0%, var(--highlight-color) 50%, var(--base-color) 100%)"
437+
/>
438+
<br />
439+
440+
<p>Narrow highlight</p>
441+
<Skeleton
442+
highlightColor="#E0B0FF"
443+
customHighlightBackground="linear-gradient(90deg, var(--base-color) 40%, var(--highlight-color) 50%, var(--base-color) 60%)"
444+
/>
445+
<br />
446+
447+
<p>Wide highlight</p>
448+
<Skeleton
449+
highlightColor="#E0B0FF"
450+
customHighlightBackground="linear-gradient(90deg, var(--base-color) 0%, var(--highlight-color) 5%, var(--highlight-color) 95%, var(--base-color) 100%)"
451+
/>
452+
<br />
453+
454+
<p>Fun gradient</p>
455+
<Skeleton
456+
highlightColor="#E0B0FF"
457+
customHighlightBackground="linear-gradient(90deg, var(--base-color) 0%, rgba(131,58,180,1) 25%, rgba(253,29,29,1) 50%, rgba(252,176,69,1) 75%, var(--base-color) 100%)"
458+
/>
459+
</div>
460+
);

src/skeleton.css

+8-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@
3232
right: 0;
3333
height: 100%;
3434
background-repeat: no-repeat;
35-
background-image: linear-gradient(
36-
90deg,
37-
var(--base-color),
38-
var(--highlight-color),
39-
var(--base-color)
35+
background-image: var(
36+
--custom-highlight-background,
37+
linear-gradient(
38+
90deg,
39+
var(--base-color) 0%,
40+
var(--highlight-color) 50%,
41+
var(--base-color) 100%
42+
)
4043
);
4144
transform: translateX(-100%);
4245

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"jsx": "react",
66

77
/* Modules */
8-
"module": "ESNext",
8+
"module": "nodenext",
99
"moduleResolution": "nodenext",
1010

1111
/* Emit */

0 commit comments

Comments
 (0)