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

added feature: absolute snap dimensions #337

Merged
merged 3 commits into from
Aug 23, 2018

Conversation

therebelrobot
Copy link
Contributor

@therebelrobot therebelrobot commented Aug 23, 2018

Proposed solution

This PR is aiming to allow defining absolute snap dimensions, rather than an infinite grid. It also allows for selective axis definition, so you can absolutely snap only the x axis, for example.

e.g.

grid={[1,5]} will land on [1, 5], [2, 5], as well as [40, 50] ad infinitum.

snap={{ x: [20, 30, 50], y: [10] }} will only land on [20, 10], [30, 10], [50, 10].

Tradeoffs

Drawbacks: This solution puts priority of the absolute snap over the grid. Alternatively, it could snap to the defined grid after the absolute numbers, but I figured that'd be an odd developer experience since they're explicitly defining it to begin with.

There is an additional function added, findClosestSnap which does a reduce on the axis array. I've tried to keep this minimal and close to the snap function for ease of finding.

There is arguably a nomenclature disconnect between grid and snap props since both are technically "snapping". snap just seemed less verbose. I can change it if desired.

I'm also fairly sure I updated the types correctly, but I'm a typescript user rather than a flowtype user, it may need some tweaking.

Testing Done

I have confirmed this works both in storybook (by adding a new story for it) and in my company's internal application under the npm module name @therebel/re-resizable. If this merges I'll update my application to use upstream ^_^

EDIT: I also added a new test to test/index.test.js:427

  1. Pull the latest master branch: This was forked from master today.
  2. If your PR fixes an issue, reference that issue: this isn't an open issue that I'm aware of. But I'll check again and update the PR with it if I find one. (EDIT: Allow controlled resizing #319 probably has some overlap with this, though that sounds like a custom logic redefinition, rather than a property addition)

Copy link
Owner

@bokuweb bokuweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 👍

@@ -135,6 +139,8 @@ type State = {
const clamp = (n: number, min: number, max: number): number => Math.max(Math.min(n, max), min);
const snap = (n: number, size: number): number => Math.round(n / size) * size;

const findClosestSnap = (n: number, snapArray: Array<number>): number => snapArray.reduce((prev, curr) => (Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@bokuweb bokuweb merged commit 2f5063e into bokuweb:master Aug 23, 2018
@bokuweb
Copy link
Owner

bokuweb commented Aug 23, 2018

I just published v4.8.0 🎉 thanks ✨

@therebelrobot
Copy link
Contributor Author

Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants