Skip to content

Commit 4beb7fb

Browse files
committed
Add ability to change state directly via refs
Closes #5
1 parent 892e00a commit 4beb7fb

File tree

4 files changed

+87
-13
lines changed

4 files changed

+87
-13
lines changed

src/composables/useWebAppBackButton.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ref } from 'vue'
1+
import { computed, ref } from 'vue'
22
import { useWebApp } from './useWebApp'
33
import type { OnBackButtonClickedCallback } from '~/types'
44

@@ -25,7 +25,14 @@ export function useWebAppBackButton() {
2525
onEvent('backButtonClicked', eventHandler)
2626

2727
return {
28-
isBackButtonVisible,
28+
isBackButtonVisible: computed({
29+
get() {
30+
return isBackButtonVisible.value
31+
},
32+
set(isVisible) {
33+
isVisible ? showBackButton() : hideBackButton()
34+
},
35+
}),
2936
onBackButtonClicked,
3037
showBackButton,
3138
hideBackButton,

src/composables/useWebAppClosingConfirmation.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ref } from 'vue'
1+
import { computed, ref } from 'vue'
22

33
const isClosingConfirmationEnabled = ref(Telegram.WebApp.isClosingConfirmationEnabled)
44

@@ -18,7 +18,14 @@ function disableClosingConfirmation(...params: Parameters<typeof Telegram.WebApp
1818

1919
export function useWebAppClosingConfirmation() {
2020
return {
21-
isClosingConfirmationEnabled,
21+
isClosingConfirmationEnabled: computed({
22+
get() {
23+
return isClosingConfirmationEnabled.value
24+
},
25+
set(isEnabled) {
26+
isEnabled ? enableClosingConfirmation() : disableClosingConfirmation()
27+
},
28+
}),
2229
enableClosingConfirmation,
2330
disableClosingConfirmation,
2431
}

src/composables/useWebAppMainButton.ts

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ref } from 'vue'
1+
import { computed, ref } from 'vue'
22
import { useWebApp } from './useWebApp'
33
import type { OnMainButtonClickedCallback } from '~/types'
44

@@ -65,12 +65,58 @@ export function useWebAppMainButton() {
6565
onEvent('mainButtonClicked', eventHandler)
6666

6767
return {
68-
mainButtonText,
69-
mainButtonColor,
70-
mainButtonTextColor,
71-
isMainButtonVisible,
72-
isMainButtonActive,
73-
isMainButtonProgressVisible,
68+
mainButtonText: computed({
69+
get() {
70+
return mainButtonText.value
71+
},
72+
set(text) {
73+
setMainButtonText(text)
74+
},
75+
}),
76+
mainButtonColor: computed({
77+
get() {
78+
return mainButtonColor.value
79+
},
80+
set(color) {
81+
setMainButtonParams({
82+
color,
83+
})
84+
},
85+
}),
86+
mainButtonTextColor: computed({
87+
get() {
88+
return mainButtonTextColor.value
89+
},
90+
set(color) {
91+
setMainButtonParams({
92+
text_color: color,
93+
})
94+
},
95+
}),
96+
isMainButtonVisible: computed({
97+
get() {
98+
return isMainButtonVisible.value
99+
},
100+
set(isVisible) {
101+
isVisible ? showMainButton() : hideMainButton()
102+
},
103+
}),
104+
isMainButtonActive: computed({
105+
get() {
106+
return isMainButtonActive.value
107+
},
108+
set(isActive) {
109+
isActive ? enableMainButton() : disableMainButton()
110+
},
111+
}),
112+
isMainButtonProgressVisible: computed({
113+
get() {
114+
return isMainButtonProgressVisible.value
115+
},
116+
set(isProgressVisible) {
117+
isProgressVisible ? showMainButtonProgress() : hideMainButtonProgress()
118+
},
119+
}),
74120
setMainButtonText,
75121
onMainButtonClicked,
76122
showMainButton,

src/composables/useWebAppTheme.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,22 @@ export function useWebAppTheme() {
3434
return {
3535
colorScheme: readonly(colorScheme),
3636
themeParams: readonly(themeParams),
37-
headerColor,
38-
backgroundColor,
37+
headerColor: computed({
38+
get() {
39+
return headerColor.value
40+
},
41+
set(newValue) {
42+
setHeaderColor(newValue)
43+
},
44+
}),
45+
backgroundColor: computed({
46+
get() {
47+
return backgroundColor.value
48+
},
49+
set(newValue) {
50+
setBackgroundColor(newValue)
51+
},
52+
}),
3953
setHeaderColor,
4054
setBackgroundColor,
4155
onThemeChanged,

0 commit comments

Comments
 (0)