Skip to content

Commit

Permalink
Add $landscape and $portrait stores to trackScreenOrientation i…
Browse files Browse the repository at this point in the history
…ntegration (#70)
  • Loading branch information
efremenkovan authored Jan 29, 2024
1 parent db9552b commit 1a83c99
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-geese-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@withease/web-api': minor
---

Add `$landscape` and `$portrait` stores to `trackScreenOrientation` integration
2 changes: 2 additions & 0 deletions apps/web-api-demo/screen-orientation.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<h2>Screen orientation</h2>
<p>Type: <span id="type" /></p>
<p>Angle: <span id="angle" /></p>
<p>Portrait: <span id="portrait" /></p>
<p>Landscape: <span id="landscape" /></p>
</main>

<script type="module" src="/src/screen-orientation.ts"></script>
Expand Down
14 changes: 13 additions & 1 deletion apps/web-api-demo/src/screen-orientation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ const appStarted = createEvent();

const typeElement = document.querySelector('#type')!;

Check warning on line 6 in apps/web-api-demo/src/screen-orientation.ts

View workflow job for this annotation

GitHub Actions / checks

Forbidden non-null assertion
const angleElement = document.querySelector('#angle')!;

Check warning on line 7 in apps/web-api-demo/src/screen-orientation.ts

View workflow job for this annotation

GitHub Actions / checks

Forbidden non-null assertion
const portraitElement = document.querySelector('#portrait')!;

Check warning on line 8 in apps/web-api-demo/src/screen-orientation.ts

View workflow job for this annotation

GitHub Actions / checks

Forbidden non-null assertion
const landscapeElement = document.querySelector('#landscape')!;

const { $type, $angle } = trackScreenOrientation({ setup: appStarted });
const { $type, $angle, $landscape, $portrait } = trackScreenOrientation({

Check failure on line 11 in apps/web-api-demo/src/screen-orientation.ts

View workflow job for this annotation

GitHub Actions / checks

Property '$landscape' does not exist on type '{ $type: Store<OrientationType | null>; $angle: Store<number | null>; }'.

Check failure on line 11 in apps/web-api-demo/src/screen-orientation.ts

View workflow job for this annotation

GitHub Actions / checks

Property '$portrait' does not exist on type '{ $type: Store<OrientationType | null>; $angle: Store<number | null>; }'.
setup: appStarted,
});

$type.watch((type) => {
console.log('type', type);
Expand All @@ -16,5 +20,13 @@ $angle.watch((angle) => {
console.log('angle', angle);
angleElement.textContent = JSON.stringify(angle);
});
$landscape.watch((landscape) => {

Check failure on line 23 in apps/web-api-demo/src/screen-orientation.ts

View workflow job for this annotation

GitHub Actions / checks

Parameter 'landscape' implicitly has an 'any' type.
console.log('landscape', landscape);
landscapeElement.textContent = JSON.stringify(landscape);
});
$portrait.watch((portrait) => {

Check failure on line 27 in apps/web-api-demo/src/screen-orientation.ts

View workflow job for this annotation

GitHub Actions / checks

Parameter 'portrait' implicitly has an 'any' type.
console.log('portrait', portrait);
portraitElement.textContent = JSON.stringify(portrait);
});

appStarted();
17 changes: 15 additions & 2 deletions apps/website/docs/web-api/screen_orientation.live.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ import { onMounted } from 'vue';
const appStarted = createEvent();
const { $type, $angle } = trackScreenOrientation({ setup: appStarted });
const { $type, $angle, $portrait, $landscape } = trackScreenOrientation({
setup: appStarted,
});
const [type, angle] = useUnit([$type, $angle]);
const [type, angle, portrait, landscape] = useUnit([
$type,
$angle,
$portrait,
$landscape,
]);
onMounted(appStarted);
</script>
Expand All @@ -20,4 +27,10 @@ onMounted(appStarted);
<p>
Screen orientation angle: <strong>{{ angle }}</strong>
</p>
<p>
Is device in portait orientation: <strong>{{ portrait }}</strong>
</p>
<p>
Is device in landscape orientation: <strong>{{ landscape }}</strong>
</p>
</template>
6 changes: 4 additions & 2 deletions apps/website/docs/web-api/screen_orientation.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ All you need to do is to create an integration by calling `trackScreenOrientatio
```ts
import { trackScreenOrientation } from '@withease/web-api';

const { $type, $angle } = trackScreenOrientation({
const { $type, $angle, $portrait, $landscape } = trackScreenOrientation({
setup: appStarted,
});
```
Expand All @@ -31,6 +31,8 @@ Returns an object with:

- `$type`: [_Store_](https://effector.dev/docs/api/effector/store) with current orientation type, one of "portrait-primary", "portrait-secondary", "landscape-primary", or "landscape-secondary"
- `$angle`: [_Store_](https://effector.dev/docs/api/effector/store) with a `number` which represents the current orientation angle in degrees
- `$portrait`: [_Store_](https://effector.dev/docs/api/effector/store) with a `boolean` which states if device is in portrait orientation
- `$landscape`: [_Store_](https://effector.dev/docs/api/effector/store) with a `boolean` which states if device is in landscape orientation

::: tip
It supports [`@@trigger` protocol](/protocols/trigger). Since it allows firing only one [_Event_](https://effector.dev/en/api/effector/event/) `trackScreenOrientation` triggers any updates of `$type` as a `fired` in case of [`@@trigger` protocol](/protocols/trigger).
Expand All @@ -45,7 +47,7 @@ somethingExpectsTrigger(trackScreenOrientation);

## Live demo

Let us show you a live demo of how it works. The following demo displays `$type` and `$angle` values of the current screen orientation. _Rotate your device to see how it works._
Let us show you a live demo of how it works. The following demo displays `$type`, `$angle`, `$portrait` and `$landscape` values of the current screen orientation. _Rotate your device to see how it works._

<script setup lang="ts">
import demoFile from './screen_orientation.live.vue?raw';
Expand Down
16 changes: 15 additions & 1 deletion packages/web-api/src/screen_orientation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ const trackScreenOrientation: ScreenOrientation & TriggerProtocol = (
{ serialize: 'ignore' }
);

/**
* States if device is in landscape orientation
*/
const $landscape = $type.map((type) => {
return type === 'landscape-primary' || type === 'landscape-secondary';
});

/**
* States if device is in portrait orientation
*/
const $portrait = $type.map((type) => {
return type === 'portrait-primary' || type === 'portrait-secondary';
});

const orientationChanged = setupListener(
{
add: (listener) =>
Expand All @@ -45,7 +59,7 @@ const trackScreenOrientation: ScreenOrientation & TriggerProtocol = (
target: $angle,
});

return { $type, $angle };
return { $type, $angle, $portrait, $landscape };
};

trackScreenOrientation['@@trigger'] = () => {
Expand Down

0 comments on commit 1a83c99

Please sign in to comment.