-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(drawer): add dynamic width feature to drawer (#820)
This PR adds setting width feature into drawer. Closes #767 This had to be written with JavaScript because it was previously written with **@ media**, and we couldn't use CSS variables inside **@ media**. Reference source: https://stackoverflow.com/a/40723269 Also added style-to-px-converter to give width properties like '%', 'vh' --------- Co-authored-by: Erdem Gönül <[email protected]>
- Loading branch information
1 parent
ca06fb6
commit aef2ae8
Showing
6 changed files
with
165 additions
and
9 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
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,56 @@ | ||
import { expect } from "@open-wc/testing"; | ||
import { styleToPixelConverter } from "./style-to-px.converter"; | ||
|
||
describe("styleToPixelConverter", () => { | ||
it("converts pixel value correctly", () => { | ||
const result = styleToPixelConverter("100px"); | ||
|
||
expect(result).to.equal(100); | ||
}); | ||
|
||
it("converts viewport width value correctly", () => { | ||
Object.defineProperty(window, "innerWidth", { value: 600, writable: true }); | ||
|
||
const result = styleToPixelConverter("50vw"); | ||
|
||
expect(result).to.equal(300); // 50% of 600 (viewport width) | ||
}); | ||
|
||
it("converts percentage value correctly", () => { | ||
Object.defineProperty(window, "innerWidth", { value: 800, writable: true }); | ||
|
||
const result = styleToPixelConverter("25%"); | ||
|
||
expect(result).to.equal(200); // 25% of 800 (viewport width) | ||
}); | ||
|
||
it("returns 0 for invalid input", () => { | ||
const result = styleToPixelConverter("invalid"); | ||
|
||
expect(result).to.null; | ||
}); | ||
|
||
it("returns null for empty input", () => { | ||
const result = styleToPixelConverter(""); | ||
|
||
expect(result).to.null; | ||
}); | ||
|
||
it("returns null for input without a unit", () => { | ||
const result = styleToPixelConverter("42"); | ||
|
||
expect(result).to.null; | ||
}); | ||
|
||
it("handles decimal values correctly", () => { | ||
const result = styleToPixelConverter("12.5px"); | ||
|
||
expect(result).to.equal(12.5); | ||
}); | ||
|
||
it("returns null for unsupported unit", () => { | ||
const result = styleToPixelConverter("10em"); | ||
|
||
expect(result).to.null; | ||
}); | ||
}); |
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,26 @@ | ||
export const styleToPixelConverter = (styleValue: string): number | null => { | ||
const match = styleValue.match(/^(\d+(\.\d+)?)(.*)$/); | ||
|
||
if (!match) return null; | ||
|
||
const value = parseFloat(match[1]); | ||
const unit = match[3]; | ||
|
||
let styleInPixel: number | null; | ||
|
||
switch (unit) { | ||
case "px": | ||
styleInPixel = value; | ||
break; | ||
case "vw": | ||
styleInPixel = (value * window.innerWidth) / 100; | ||
break; | ||
case "%": | ||
styleInPixel = (value * window.innerWidth) / 100; | ||
break; | ||
default: | ||
styleInPixel = null; | ||
break; | ||
} | ||
return styleInPixel; | ||
}; |