Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom ancestor container #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ const focusedImage = new FocusedImage(img, {
})
```

#### Custom Ancestor Container

Use the `container` option to pass in a custom ancestor. This can come in handy if you use the `<picture>` tag in your HTML.

```html
<div class="focused-image-container">
<picture>
<!-- ... -->
<img class="focused-image" src="https://picsum.photos/2400/1400">
</picture>
</div>
```

```ts
import { FocusedImage } from "image-focus"

const img = document.querySelector('.focused-image') as HTMLImageElement
const container = document.querySelector('.focused-image-container') as HTMLElement

const focusedImage = new FocusedImage(img, {
container: container
})
```

### FocusPicker

Provide an `onChange` callback that will receive a `Focus` object that has `x` and `y` properties for the newly selected coordinates. Optionally supply a `focus` to initialize with, or a `retina` src to use instead of the default white ring SVG.
Expand Down
4 changes: 3 additions & 1 deletion src/FocusedImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export class FocusedImage {

// Set up element references
this.img = imageNode;
this.container = imageNode.parentElement;
this.container = this.options.container
? this.options.container
: imageNode.parentElement;

// Set up instance
if (this.img['__focused_image_instance__']) {
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ export interface FocusedImageOptions {
* Default value is "relative"
*/
containerPosition?: 'fixed' | 'relative' | 'absolute' | 'sticky';
/**
* Focus image container
*
* Default value is `undefined`, and code will fall back to `FocusedImage::imageNode.parentElement`
*/
container?: HTMLElement;
}
16 changes: 16 additions & 0 deletions test/FocusedImage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ describe('FocusedImage', () => {
<div class="focused-image-container top-left">
<img class="focused-image" src="https://picsum.photos/2400/1400" alt="" data-focus-x="0.28" data-focus-y="0.33">
</div>
<div class="pic-focused-image-container top-left">
<picture class="pic-container">
<img class="pic-focused-image" src="https://picsum.photos/2400/1400" alt="" data-focus-x="0.28" data-focus-y="0.33">
<picture>
</div>
</body>
`;
});
Expand Down Expand Up @@ -66,4 +71,15 @@ describe('FocusedImage', () => {
expect(instance.img).toBeTruthy();
expect(instance.container).toBeTruthy();
});

it('should use given custom container', () => {
const img = document.querySelector(
'.pic-focused-image'
) as HTMLImageElement;
const container = document.querySelector(
'.pic-focused-image-container'
) as HTMLElement;
const instance = new FocusedImage(img, { container: container });
expect(instance.container).toBe(container);
});
});