-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from gzimbron/resizable-columns
feat(DataGrid): ✨ Add resizable columns
- Loading branch information
Showing
9 changed files
with
182 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"website": patch | ||
--- | ||
|
||
Add resizable columns example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@gzim/svelte-datagrid": minor | ||
--- | ||
|
||
Add resizable columns feature |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { Action } from 'svelte/action'; | ||
|
||
interface ActionParams { | ||
resizable?: boolean; | ||
startResize?: () => void; | ||
endResize?: () => void; | ||
onResize?: (data: number) => void; | ||
} | ||
|
||
export const reziseColumn: Action<HTMLElement, ActionParams> = ( | ||
node, | ||
{ resizable, onResize, startResize, endResize }: ActionParams = {} | ||
) => { | ||
if (!resizable) return; | ||
|
||
let lastX = 0; | ||
|
||
const mouseMoveEvent = (e: MouseEvent) => { | ||
if (!onResize) return; | ||
if (e.clientX === lastX) return; | ||
lastX = e.clientX; | ||
onResize(lastX - node.getBoundingClientRect().left); | ||
}; | ||
|
||
const mouseDownEvent = (e: MouseEvent) => { | ||
if (e.button !== 0) { | ||
return; | ||
} | ||
lastX = e.clientX; | ||
|
||
// get node width and style ::after | ||
const nodeAfterWidth = parseInt( | ||
window.getComputedStyle(node, ':after').width.replace('px', '') | ||
); | ||
const clickPosition = e.clientX - node.getBoundingClientRect().left; | ||
|
||
if (clickPosition < node.offsetWidth - nodeAfterWidth - 2) return; | ||
|
||
startResize && startResize(); | ||
|
||
document.addEventListener('mousemove', mouseMoveEvent); | ||
document.addEventListener('mouseup', mouseUpEvent); | ||
}; | ||
|
||
const mouseUpEvent = () => { | ||
endResize && endResize(); | ||
document.removeEventListener('mousemove', mouseMoveEvent); | ||
}; | ||
|
||
node.addEventListener('mousedown', mouseDownEvent); | ||
|
||
return { | ||
destroy() { | ||
document.removeEventListener('mousedown', mouseDownEvent); | ||
node.removeEventListener('mouseup', () => mouseUpEvent); | ||
} | ||
}; | ||
}; |
Oops, something went wrong.