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

feat: implement hairlinewidth #250

Merged
merged 1 commit into from
Jul 20, 2024
Merged
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
1 change: 1 addition & 0 deletions docs/src/content/docs/reference/unistyles-runtime.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ and use it anywhere in your code, even outside a component.
| <Badge label="2.4.0" />| navigationBar | \{width: number, height: number\} | Navigation bar dimensions (Android) |
| <Badge label="2.8.0" />| pixelRatio | number | Pixel density of the device |
| <Badge label="2.8.0" />| fontScale | number | Font scale of the device |
| <Badge label="2.9.0" />| hairlineWidth | number | The thinnest width of the platform |

## Setters

Expand Down
11 changes: 10 additions & 1 deletion examples/expo/src/examples/RuntimeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const RuntimeScreen: React.FunctionComponent = () => {
statusBar,
navigationBar,
pixelRatio,
fontScale
fontScale,
hairlineWidth
} = UnistylesRuntime
const { styles, theme } = useStyles(stylesheet)

Expand Down Expand Up @@ -74,6 +75,14 @@ export const RuntimeScreen: React.FunctionComponent = () => {
{fontScale}
</Text>
</View>
<View style={styles.row}>
<Text style={styles.text(true)}>
Hairline width:
</Text>
<Text style={styles.text(false)}>
{hairlineWidth}
</Text>
</View>
<View style={styles.row}>
<Text style={styles.text(true)}>
Insets:
Expand Down
3 changes: 3 additions & 0 deletions src/core/UnistylesModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class UnistylesBridgeWeb {
}
#pixelRatio = 1.0
#fontScale = 1.0
#hairlineWidth = 0.333333

constructor() {
if (!isServer) {
Expand Down Expand Up @@ -85,6 +86,8 @@ export class UnistylesBridgeWeb {
return this.#pixelRatio
case 'fontScale':
return this.#fontScale
case 'hairlineWidth':
return this.#hairlineWidth
case 'useTheme':
return (themeName: keyof UnistylesThemes) => this.useTheme(themeName)
case 'updateTheme':
Expand Down
14 changes: 13 additions & 1 deletion src/core/UnistylesRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class UnistylesRuntime {
},
orientation: this.orientation,
pixelRatio: this.pixelRatio,
fontScale: this.fontScale
fontScale: this.fontScale,
hairlineWidth: this.hairlineWidth
}
}

Expand Down Expand Up @@ -174,6 +175,17 @@ export class UnistylesRuntime {
return parseFloat(this.unistylesBridge.fontScale.toFixed(2))
}

/**
* Get the hairline width
* @returns - The thinnest width of the platform
*/
public get hairlineWidth() {
const pixelRatio = this.pixelRatio
const nearestPixel = Math.trunc(pixelRatio * 0.4) || 1

return nearestPixel / pixelRatio
}

/**
* Get the immersive mode (both status bar and navigation bar hidden (Android))
* @param isEnabled
Expand Down
7 changes: 6 additions & 1 deletion src/core/mocks/UnistylesMockedRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class UnistylesMockedRuntime {
},
orientation: this.orientation,
pixelRatio: this.pixelRatio,
fontScale: this.fontScale
fontScale: this.fontScale,
hairlineWidth: this.hairlineWidth
}
}

Expand Down Expand Up @@ -90,6 +91,10 @@ export class UnistylesMockedRuntime {
return 1.0
}

public get hairlineWidth() {
return 0.333333
}

public get fontScale() {
return 1.0
}
Expand Down
Loading