Skip to content

Commit

Permalink
Merge pull request #233 from dvtng/srmagura/highlight-width
Browse files Browse the repository at this point in the history
Custom highlight background
  • Loading branch information
dvtng authored Sep 21, 2024
2 parents d8c1492 + c097345 commit f8b040d
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 3.5.0

### Features

- Add optional `customHighlightBackground` prop. (#233)

## 3.4.0

### Features
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ return (
</td>
<td><code>true</code></td>
</tr>
<tr>
<td><code>customHighlightBackground?: string</code></td>
<td>
Allows you to override the <code>background-image</code> property of the highlight element, enabling you to fully customize the gradient. See example below.
</td>
<td><code>undefined</code></td>
</tr>
</tbody>
</table>

Expand Down Expand Up @@ -273,6 +280,18 @@ const wrapped2 = (
);
```

### Custom Highlight Background

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:

```tsx
<Skeleton customHighlightBackground="linear-gradient(90deg, var(--base-color) 40%, var(--highlight-color) 50%, var(--base-color) 60%)" />
```

**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.

![Custom highlight background example](assets/custom-highlight-background.png)

## Troubleshooting

### The skeleton width is 0 when the parent has `display: flex`!
Expand Down
Binary file added assets/custom-highlight-background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-loading-skeleton",
"version": "3.4.0",
"version": "3.5.0",
"description": "Make beautiful, animated loading skeletons that automatically adapt to your app.",
"keywords": [
"react",
Expand Down
5 changes: 5 additions & 0 deletions src/Skeleton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function styleOptionsToCssProperties({
direction,
duration,
enableAnimation = defaultEnableAnimation,

customHighlightBackground,
}: SkeletonStyleProps & { circle: boolean }): CSSProperties &
Record<`--${string}`, string> {
const style: ReturnType<typeof styleOptionsToCssProperties> = {};
Expand All @@ -41,6 +43,9 @@ function styleOptionsToCssProperties({
if (typeof highlightColor !== 'undefined')
style['--highlight-color'] = highlightColor;

if (typeof customHighlightBackground === 'string')
style['--custom-highlight-background'] = customHighlightBackground;

return style;
}

Expand Down
2 changes: 2 additions & 0 deletions src/SkeletonStyleProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ export interface SkeletonStyleProps {
duration?: number;
direction?: 'ltr' | 'rtl';
enableAnimation?: boolean;

customHighlightBackground?: string;
}
31 changes: 31 additions & 0 deletions src/__stories__/Skeleton.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,34 @@ export const PrefersReducedMotion = () => (
/>
</div>
);

export const HighlightWidth = () => (
<div style={{ width: 500 }}>
<p>Default</p>
<Skeleton
highlightColor="#E0B0FF"
customHighlightBackground="linear-gradient(90deg, var(--base-color) 0%, var(--highlight-color) 50%, var(--base-color) 100%)"
/>
<br />

<p>Narrow highlight</p>
<Skeleton
highlightColor="#E0B0FF"
customHighlightBackground="linear-gradient(90deg, var(--base-color) 40%, var(--highlight-color) 50%, var(--base-color) 60%)"
/>
<br />

<p>Wide highlight</p>
<Skeleton
highlightColor="#E0B0FF"
customHighlightBackground="linear-gradient(90deg, var(--base-color) 0%, var(--highlight-color) 5%, var(--highlight-color) 95%, var(--base-color) 100%)"
/>
<br />

<p>Fun gradient</p>
<Skeleton
highlightColor="#E0B0FF"
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%)"
/>
</div>
);
13 changes: 8 additions & 5 deletions src/skeleton.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@
right: 0;
height: 100%;
background-repeat: no-repeat;
background-image: linear-gradient(
90deg,
var(--base-color),
var(--highlight-color),
var(--base-color)
background-image: var(
--custom-highlight-background,
linear-gradient(
90deg,
var(--base-color) 0%,
var(--highlight-color) 50%,
var(--base-color) 100%
)
);
transform: translateX(-100%);

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"jsx": "react",

/* Modules */
"module": "ESNext",
"module": "nodenext",
"moduleResolution": "nodenext",

/* Emit */
Expand Down

0 comments on commit f8b040d

Please sign in to comment.