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

docs: Implemented "recipe" page #791

Merged
merged 9 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
45 changes: 45 additions & 0 deletions apps/docs/docs/learn/06-recipes/01-contexual-styles.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---

Check warning on line 1 in apps/docs/docs/learn/06-recipes/01-contexual-styles.mdx

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"contexual" should be "contextual".
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
sidebar_position: 5
aminaopio marked this conversation as resolved.
Show resolved Hide resolved
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';


# Contextual styles

Contextual styles are an effective way to dynamically adapt styles based on a component's state or its environment. Here are some approaches to achieve this:

### Using Context

Context can help reduce prop-drilling by sharing state across components. For example, you can manage the open or closed state of a sidebar using React Context and conditionally apply styles:

```tsx
import { createContext, useContext } from 'react';
import * as stylex from '@stylexjs/stylex';

const SidebarContext = createContext(false);
aminaopio marked this conversation as resolved.
Show resolved Hide resolved

const styles = stylex.create({
sidebarOpen: {
width: '250px',
},
sidebarClosed: {
width: '50px',
},
});
aminaopio marked this conversation as resolved.
Show resolved Hide resolved

function Sidebar({ children }) {
const isOpen = useContext(SidebarContext);
return (
<div {...stylex.props(isOpen ? styles.sidebarOpen : styles.sidebarClosed)}>
{children}
</div>
);
}
```

47 changes: 47 additions & 0 deletions apps/docs/docs/learn/06-recipes/02-variants.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
sidebar_position: 5
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Variants

The "variants" pattern allows you to conditionally apply one of several predefined styles based on a value. This is especially useful for theming or dynamic component behavior.

### Example: Button Variants

Here’s how you can create a button component with different visual styles based on a `variant` prop:

```tsx
import * as stylex from '@stylexjs/stylex';

const styles = stylex.create({
primary: {
backgroundColor: {
default: 'blue',
':hover': 'darkblue',
},
color: 'white',
},
secondary: {
backgroundColor: {
default: 'gray',
':hover': 'darkgray',
},
color: 'white',
},
});

function Button({ variant = 'primary', ...props }) {
return <button {...props} {...stylex.props(styles[variant], props.style)} />;
}
aminaopio marked this conversation as resolved.
Show resolved Hide resolved

// Usage
<Button variant="primary">Primary</Button>
<Button variant="secondary">Secondary</Button>
```
46 changes: 46 additions & 0 deletions apps/docs/docs/learn/06-recipes/03-descendant styles.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Descendant styles

## Hover-Dependent Descendant Styles

Using CSS variables, you can style descendants based on a parent's state, such as `:hover`.

### Example: Nested Hover Styles

In this example, we'll apply a hover effect to a child element when the parent is hovered:

`variables.stylex.ts` defines a variable using `stylex.defineVars`:

```tsx
import * as stylex from '@stylexjs/stylex';

export const vars = stylex.defineVars({
childColor: 'black',
});
```
In the `styles.parent` object, the variable `vars.childColor` is referenced and updated on hover.

```tsx
import * as stylex from '@stylexjs/stylex';
import { vars } from './variables.stylex';

const styles = stylex.create({
parent: {
[vars.childColor]: vars.childColor,
':hover': {
[vars.childColor]: 'blue',
},
},
child: {
color: `var(${vars.childColor})`,
aminaopio marked this conversation as resolved.
Show resolved Hide resolved
},
});

function ParentWithHover() {
return (
<div {...stylex.props(styles.parent)}>
<span {...stylex.props(styles.child)}>Hover over me!</span>
</div>
);
}

```
4 changes: 4 additions & 0 deletions apps/docs/docs/learn/06-recipes/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "Recipes",
"position": 4
}
Loading