-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: revamp layout props (minify syntax)
- Loading branch information
1 parent
05b28fe
commit fe2c3c3
Showing
6 changed files
with
104 additions
and
110 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 was deleted.
Oops, something went wrong.
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,47 @@ | ||
import type { AspectRatio } from '../types/layout'; | ||
|
||
const aspectRatioValues: { [key: string]: number | string } = { | ||
auto: 'auto', | ||
square: 1, | ||
video: 16 / 9, | ||
}; | ||
|
||
const getAspectRatioValue = (key: string | number): number | string => { | ||
if (typeof key === 'number') return key; | ||
if (key === 'auto') return 'auto'; | ||
if (!isNaN(Number(key))) return Number(key); | ||
|
||
// Handle fractional strings like '1/3' | ||
const fractionMatch = key.match(/^(\d+)\/(\d+)$/); | ||
if (fractionMatch) { | ||
const numerator = Number(fractionMatch[1]); | ||
const denominator = Number(fractionMatch[2]); | ||
return numerator / denominator; | ||
} | ||
|
||
const value = aspectRatioValues[key]; | ||
if (value === undefined) throw new Error(`Invalid aspect ratio key: ${key}`); | ||
return value; | ||
}; | ||
|
||
const aspect = { | ||
custom_: (key: string | number): AspectRatio => { | ||
return { aspectRatio: getAspectRatioValue(key) }; | ||
}, | ||
} as { [key: string]: AspectRatio } & { | ||
custom_: (key: string | number) => AspectRatio; | ||
}; | ||
|
||
Object.keys(aspectRatioValues).forEach((key) => { | ||
aspect[key] = { aspectRatio: getAspectRatioValue(key) }; | ||
}); | ||
|
||
// Example usage | ||
// const autoAspectRatio = aspect.auto; // { aspectRatio: 'auto' } | ||
// const squareAspectRatio = aspect.square; // { aspectRatio: 1 } | ||
// const videoAspectRatio = aspect.video; // { aspectRatio: 16 / 9 } | ||
// const customAspectRatio = aspect.custom_('4/3'); // { aspectRatio: 4 / 3 } | ||
// const customAspectRatioNumber = aspect.custom_(3); // { aspectRatio: 3 } | ||
// const customAspectRatioString = aspect.custom_('3'); // { aspectRatio: 3 } | ||
|
||
export default aspect; |
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 |
---|---|---|
@@ -1,20 +1,27 @@ | ||
const zIndex = { | ||
z_0: { zIndex: 0 }, | ||
z_10: { zIndex: 10 }, | ||
z_20: { zIndex: 20 }, | ||
z_30: { zIndex: 30 }, | ||
z_40: { zIndex: 40 }, | ||
z_50: { zIndex: 50 }, | ||
z_auto: { zIndex: 'auto' }, | ||
const z = { | ||
index_0: { zIndex: 0 }, | ||
index_10: { zIndex: 10 }, | ||
index_20: { zIndex: 20 }, | ||
index_30: { zIndex: 30 }, | ||
index_40: { zIndex: 40 }, | ||
index_50: { zIndex: 50 }, | ||
index_auto: { zIndex: 'auto' }, | ||
|
||
index_(value: number | string): { zIndex: number | 'auto' } { | ||
if (value === 'auto') { | ||
return { zIndex: 'auto' }; | ||
} else if (!isNaN(Number(value))) { | ||
return { zIndex: Number(value) }; | ||
} else { | ||
throw new Error('Invalid zIndex value'); | ||
} | ||
}, | ||
}; | ||
|
||
// Example usage | ||
// console.log(zIndex.z_0); // { zIndex: 0 } | ||
// console.log(zIndex.z_10); // { zIndex: 10 } | ||
// console.log(zIndex.z_20); // { zIndex: 20 } | ||
// console.log(zIndex.z_30); // { zIndex: 30 } | ||
// console.log(zIndex.z_40); // { zIndex: 40 } | ||
// console.log(zIndex.z_50); // { zIndex: 50 } | ||
// console.log(zIndex.z_auto); // { zIndex: 'auto' } | ||
// console.log(z.index_(10)); // { zIndex: 10 } | ||
// console.log(z.index_('auto')); // { zIndex: 'auto' } | ||
// console.log(z.index_('20')); // { zIndex: 20 } | ||
// console.log(z.index_('invalid')); // Error: Invalid zIndex value | ||
|
||
export default zIndex; | ||
export default z; |