Skip to content

Commit

Permalink
docs(sdk): add Secondary Button docs
Browse files Browse the repository at this point in the history
  • Loading branch information
heyqbnk committed Oct 1, 2024
1 parent 9607445 commit c3ddb74
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/docs/.vitepress/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const packagesSidebar: Sidebar = {
scope('mini-app'),
scope('popup'),
scope('qr-scanner', 'QR Scanner'),
scope('secondary-button'),
scope('settings-button'),
scope('swipe-behavior'),
scope('theme-params'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# Secondary Button

The 💠[component](../scopes.md) responsible for the Telegram Mini Apps Secondary Button.

## Checking Support

To check if the Secondary Button is supported by the current Telegram Mini Apps version, use the
`isSupported` method:

::: code-group

```ts [Variable]
import { secondaryButton } from '@telegram-apps/sdk';

secondaryButton.isSupported(); // boolean
```

```ts [Functions]
import { isSecondaryButtonSupported } from '@telegram-apps/sdk';

isSecondaryButtonSupported(); // boolean
```

:::

## Mounting

Before using this component, it is necessary to mount it to work with properly configured
properties. To do so, use the `mount` method. It will update the `isMounted` signal property.

::: code-group

```ts [Variable]
import { secondaryButton } from '@telegram-apps/sdk';

secondaryButton.mount();
secondaryButton.isMounted(); // true
```

```ts [Functions]
import {
mountSecondaryButton,
isSecondaryButtonMounted,
} from '@telegram-apps/sdk';

mountSecondaryButton();
isSecondaryButtonMounted(); // true
```

:::

> [!INFO]
> To extract correctly configured values from theme parameters, this method also mounts
> the [Theme Params](theme-params.md) scope.
To unmount, use the `unmount` method:

::: code-group

```ts [Variable]
secondaryButton.unmount();
secondaryButton.isMounted(); // false
```

```ts [Functions]
import {
unmountSecondaryButton,
isSecondaryButtonMounted,
} from '@telegram-apps/sdk';

unmountSecondaryButton();
isSecondaryButtonMounted(); // false
```

:::

## Settings Properties

To update the button properties, use the `setParams` method. It accepts an object with optional
properties, each responsible for its own button trait.

In turn, calling this method updates such signals
as `backgroundColor`, `hasShineEffect`, `isVisible`, `isEnabled`, `isLoaderVisible`, `position`,
`state`, `textColor` and `text`.

::: code-group

```ts [Variable]
secondaryButton.setParams({
backgroundColor: '#000000',
hasShineEffect: true,
isEnabled: true,
isLoaderVisible: true,
isVisible: true,
position: 'top',
text: 'My text',
textColor: '#ffffff'
});
secondaryButton.backgroundColor(); // '#000000'
secondaryButton.hasShineEffect(); // true
secondaryButton.isEnabled(); // true
secondaryButton.isLoaderVisible(); // true
secondaryButton.isVisible(); // true
secondaryButton.position(); // 'top'
secondaryButton.text(); // 'My text'
secondaryButton.textColor(); // '#ffffff'

secondaryButton.state();
// {
// backgroundColor: '#000000',
// hasShineEffect: true,
// isActive: true,
// isLoaderVisible: true,
// isVisible: true,
// position: 'top',
// text: 'My text',
// textColor: '#ffffff'
// }
```

```ts [Functions]
import {
setSecondaryButtonParams,
secondaryButtonBackgroundColor,
secondaryButtonHasShineEffect,
isSecondaryButtonVisible,
isSecondaryButtonEnabled,
isSecondaryButtonLoaderVisible,
secondaryButtonState,
secondaryButtonTextColor,
secondaryButtonText,
secondaryButtonPosition,
} from '@telegram-apps/sdk';

setSecondaryButtonParams({
backgroundColor: '#000000',
hasShineEffect: true,
isEnabled: true,
isLoaderVisible: true,
isVisible: true,
position: 'top',
text: 'My text',
textColor: '#ffffff'
});
secondaryButtonBackgroundColor(); // '#000000'
secondaryButtonHasShineEffect(); // true
isSecondaryButtonEnabled(); // true
isSecondaryButtonLoaderVisible(); // true
isSecondaryButtonVisible(); // true
secondaryButtonPosition(); // 'top'
secondaryButtonText(); // 'My text'
secondaryButtonTextColor(); // '#ffffff'

secondaryButtonState();
// {
// backgroundColor: '#000000',
// hasShineEffect: true,
// isActive: true,
// isLoaderVisible: true,
// isVisible: true,
// position: 'top',
// text: 'My text',
// textColor: '#ffffff'
// }
```

:::

## Tracking Click

To add a button click listener, use the `onClick` method. It returns a function to remove the bound
listener. Alternatively, you can use the `offClick` method.

::: code-group

```ts [Variable]
function listener() {
console.log('Clicked!');
}

const offClick = secondaryButton.onClick(listener);
offClick();
// or
secondaryButton.onClick(listener);
secondaryButton.offClick(listener);
```

```ts [Functions]
import {
onSecondaryButtonClick,
offSecondaryButtonClick,
} from '@telegram-apps/sdk';

function listener() {
console.log('Clicked!');
}

const offClick = onSecondaryButtonClick(listener);
offClick();
// or
onSecondaryButtonClick(listener);
offSecondaryButtonClick(listener);
```

:::

0 comments on commit c3ddb74

Please sign in to comment.