Skip to content

Commit

Permalink
docs[197]: useResponsiveValue documentation (#3344)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahgm authored Sep 20, 2023
1 parent 58e9324 commit 6f4953b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tender-eggs-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marigold/docs": patch
---

docs[197]: useResponsiveValue documentation
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useResponsiveValue } from '@marigold/system';

export default () => {
const value = useResponsiveValue(
['no breakpoint', 'sm', 'md', 'lg', 'xl', '2xl'],
2
);
return (
<div className="sm:bg-lime-600 md:bg-lime-500 lg:bg-lime-400 xl:bg-lime-300 2xl:bg-lime-200">
{value}
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: useResponsiveValue
caption: Hook that returns values based on screen sizes.
badge: updated
---

The `useResponsiveValue` is a client side hook. It can be used to return values based on the current screen size, using breakpoints from the theme `theme.screens`.

To add breakpoints to your theme you have to write them as an array.
You can pass the following parameters to your useResponsiveValue hook:

The first parameter of the hook will be the values as array which defines the breakpoints. The second argument must be the `defaultIndex` which points opn the default value.

If you want to have a look on all possible breakpoints from the theme you can have a look [here](../introduction/design-tokens#breakpoints).

Also with Tailwind some new class names came along which you can use to add styles by using the responsive modifiers (like: sm:text-sm).

## Usage

### Import

To import the hook you just have to use this code below.

```tsx
import { useResponsiveValue } from '@marigold/system';
```

## Examples

With the use of Tailwind CSS, some classes came along with which makes it easy to use different styles on different screen sizes. Here you can see a combination of the `useResponsiveValue` hook and the Tailwind screen sizes.

For example if you want to change `background-color` for different screen sizes you have to use this code below.
You can see that according to the screen size the text value will change. You can try out yourself and resize the screen.

<ComponentDemo file="./tailwind-screen-size-with-hook.demo.tsx" />

### useSmallScreen

We also have a function that checks if the window is on a small screen size. This can be used if you just want to change something just on small screen size.

The example code shows that when it is a small screen the display css value will change.

```tsx
import { useSmallScreen } from '@marigold/system';

const isSmallScreen = useSmallScreen();
...
<div className={cn(isSmallScreen ? 'hidden' : 'block')}>random div</div>
```
2 changes: 1 addition & 1 deletion docs/theme/components/Badge.styles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ThemeComponent, cva } from '@marigold/system';

export const Badge: ThemeComponent<'Badge'> = cva(
'inline-flex items-center whitespace-nowrap rounded-[20px] px-2 py-[2px]',
'inline-flex items-center truncate rounded-[20px] px-2 py-[2px]',
{
variants: {
variant: {
Expand Down

2 comments on commit 6f4953b

@vercel
Copy link

@vercel vercel bot commented on 6f4953b Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

marigold-docs – ./

marigold-docs-marigold.vercel.app
marigold-docs.vercel.app
marigold-docs-git-main-marigold.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 6f4953b Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

marigold-storybook – ./

marigold-storybook-git-main-marigold.vercel.app
marigold-latest.vercel.app
marigold-storybook-marigold.vercel.app

Please sign in to comment.