Skip to content

Commit e7e74dc

Browse files
authored
Merge pull request #9 from BrianMitchL/v2
v2
2 parents 3f1a9c8 + 44bd1ff commit e7e74dc

14 files changed

+1462
-1061
lines changed

.eslintrc.cjs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
module.exports = {
22
root: true,
3-
parser: "@typescript-eslint/parser",
43
extends: [
54
"eslint:recommended",
65
"plugin:@typescript-eslint/recommended",
76
"plugin:testing-library/dom",
7+
"plugin:svelte/recommended",
88
"prettier",
99
],
10-
plugins: ["svelte3", "@typescript-eslint"],
11-
ignorePatterns: ["*.cjs"],
12-
overrides: [{ files: ["*.svelte"], processor: "svelte3/svelte3" }],
13-
settings: {
14-
"svelte3/typescript": () => require("typescript"),
15-
},
10+
parser: "@typescript-eslint/parser",
11+
plugins: ["@typescript-eslint"],
1612
parserOptions: {
1713
sourceType: "module",
1814
ecmaVersion: 2020,
15+
extraFileExtensions: [".svelte"],
1916
},
2017
env: {
2118
browser: true,
2219
es2017: true,
2320
node: true,
2421
},
22+
overrides: [
23+
{
24+
files: ["*.svelte"],
25+
parser: "svelte-eslint-parser",
26+
parserOptions: {
27+
parser: "@typescript-eslint/parser",
28+
},
29+
},
30+
],
2531
};

README.md

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,83 @@
1-
# svelte-compare-image
1+
# svelte-compare-image [![npm](https://img.shields.io/npm/v/svelte-compare-image)](https://www.npmjs.com/package/svelte-compare-image)
22

3-
A Svelte component to compare two images.
3+
A Svelte component to compare two images with a slider to reveal one over the other.
44
Find the package on NPM at [svelte-compare-image](https://npmjs.com/package/svelte-compare-image).
55

66
An interactive example can be found at https://projects.brianm.me/svelte-compare-image/
77

8-
![screen recording example](https://github.com/BrianMitchL/svelte-compare-image/raw/main/example.gif)
8+
This component is CI tested against Svelte 4, but should work with Svelte 3 as well.
99

10-
The markup, state logic, and styling were originally adapted from [react-compare-image](https://github.com/junkboy0315/react-compare-image).
10+
![screen recording example](example.gif)
11+
12+
Changes are run inside of `requestAnimationFrame` to improve performance and reduce lag.
1113

1214
## Docs
1315

14-
The component will display the images and fill available width and height using a ResizeObserver according to the aspect ratios of the images.
16+
The component will display the images and fill available width and height the aspect ratios of the first image.
1517

1618
To use it, render the component as seen below, providing image src and alt text for the left and right images.
1719
The following CSS custom properties are optional and can be set to customize the appearance of the slider.
1820

19-
| Property | Default Value |
20-
| ---------------- | ------------- |
21-
| `--handle-size` | `2.5rem` |
22-
| `--slider-color` | `#ffffff` |
23-
| `--slider-width` | `0.125rem` |
21+
### Props
22+
23+
| Property | Default Value |
24+
| --------------------------- | -------------------- |
25+
| `--handle-size` | `2.5rem` |
26+
| `--handle-background-color` | `rgba(0, 0, 0, 0.6)` |
27+
| `--handle-background-image` | _see below_ |
28+
| `--handle-border-width` | `0.125rem` |
29+
| `--slider-color` | `#ffffff` |
30+
| `--slider-width` | `0.125rem` |
31+
32+
Default thumb background image:
33+
34+
```css
35+
url('data:image/svg+xml;utf8,<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M3 12H21M3 12L7 8M3 12L7 16M21 12L17 16M21 12L17 8" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>')
36+
```
37+
38+
It looks like this (shown with `darkgray` for viewing here, it is white in the component)
39+
40+
![slider icon](slider-icon.svg)
41+
42+
### Slots
43+
44+
`CompareImage` has one named slot, `slider-label`, which is used to label the
45+
slider control. It is displayed in a visually hidden `span`. The default value
46+
is the following:
47+
48+
> Set the visibility of one image over the other. 0 is full visibility of
49+
> the second image and 100 is full visibility of the first image. Any
50+
> amount in-between is a left/right cutoff at the percentage of the
51+
> slider.
52+
53+
## Example
2454

2555
```svelte
2656
<script lang="ts">
2757
import { CompareImage } from "svelte-compare-image";
2858
// or
2959
import CompareImage from "svelte-compare-image/CompareImage.svelte";
60+
61+
// too many quotes to deal with, storing as a variable is easier
62+
const handleBackgroundImage = `url('data:image/svg+xml;utf8,<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M3 12H21M3 12L7 8M3 12L7 16M21 12L17 16M21 12L17 8" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>')`;
3063
</script>
3164
3265
<CompareImage
33-
imageLeftSrc="https://via.placeholder.com/600x400/ffaa00/ffffff/?text=Left"
66+
imageLeftSrc="https://via.placeholder.com/600x400/ffaa00/ffffff/?text=Example+Text"
3467
imageLeftAlt="left"
35-
imageRightSrc="https://via.placeholder.com/600x400/00aaff/ffffff?text=Right"
68+
imageRightSrc="https://via.placeholder.com/600x400/00aaff/ffffff?text=Example+Text"
3669
imageRightAlt="right"
3770
--handle-size="2.5rem"
71+
--handle-background-color="rgba(0, 0, 0, 0.6)"
72+
--handle-background-image={handleBackgroundImage}
73+
--handle-border-width="0.125rem"
3874
--slider-color="#ffffff"
3975
--slider-width="0.125rem"
40-
/>
76+
>
77+
<svelte:fragment slot="slider-label">
78+
Set the visibility of one image over the other. 0 is full visibility of the
79+
second image and 100 is full visibility of the first image. Any amount
80+
in-between is a left/right cutoff at the percentage of the slider.
81+
</svelte:fragment>
82+
</CompareImage>
4183
```

example.gif

-179 KB
Loading

0 commit comments

Comments
 (0)