Skip to content

Commit

Permalink
DST-450: refactoring, add DemoLink component
Browse files Browse the repository at this point in the history
  • Loading branch information
aromko committed May 24, 2024
1 parent aa22517 commit f7d456a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 35 deletions.
13 changes: 0 additions & 13 deletions src/components/BackButton.tsx

This file was deleted.

13 changes: 13 additions & 0 deletions src/components/DemoLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Link} from '@tanstack/react-router';

const DemoLink = ({ destination, text = 'View demo'}: {destination: string, text?: string}) => {
return (
<div className="pb-2">
<Link target="_blank" to={destination} search="">
{text}
</Link>
</div>
);
}

export default DemoLink;
2 changes: 0 additions & 2 deletions src/components/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import BreadcrumbsExample from './compoundComponents/Breadcrumbs/BreadcrumbsExam
import TabsExample from './compoundComponents/Tabs/TabsExample';
import ServerStateExample from './state-management/ServerState/ServerStateExample';
import {Route} from '../routes/$component.preview.$example';
import BackButton from './BackButton';

const components = {
BreadcrumbsExample,
Expand All @@ -16,7 +15,6 @@ const Preview = () => {

return (
<article className="article-content">
<BackButton />
<PreviewComponent/>
</article>
)
Expand Down
10 changes: 0 additions & 10 deletions src/components/compoundComponents/Breadcrumbs/breadcrumbs.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import BreadcrumbsExample from './BreadcrumbsExample';

<CH.Scrollycoding>

```tsx BreadcrumbsExample.tsx
Expand Down Expand Up @@ -61,8 +59,6 @@ import BreadcrumbsExample from './BreadcrumbsExample';

---

## Connect the components

As final step we need to connect these components.

```tsx Breadcrumbs.tsx focus=34
Expand All @@ -74,12 +70,8 @@ import BreadcrumbsExample from './BreadcrumbsExample';

---

## Update BreadcrumbsExample.tsx

Finally we can use this compound component in our BreadcrumbsExample.tsx.



```tsx BreadcrumbsExample.tsx
// from ./BreadcrumbsExample.tsx
```
Expand All @@ -101,5 +93,3 @@ import BreadcrumbsExample from './BreadcrumbsExample';
```

</CH.Scrollycoding>

<div style={{ height: 150 }} />
4 changes: 0 additions & 4 deletions src/components/compoundComponents/Tabs/tabs.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import TabsExample from './TabsExample';

<CH.Scrollycoding>

```tsx Tabs.tsx focus=5
Expand Down Expand Up @@ -51,5 +49,3 @@ import TabsExample from './TabsExample';
Note: You can add any other elements you want like the `div`. You can also use a completely different arrangement of the elements.

</CH.Scrollycoding>

<div style={{ height: 300 }} />
12 changes: 9 additions & 3 deletions src/components/compoundComponents/index.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BreadcrumbDoc from './Breadcrumbs/breadcrumbs.mdx';
import TabsDoc from './Tabs/tabs.mdx';
import { Link } from '@tanstack/react-router';
import DemoLink from '../DemoLink';

# Compound components in React

Expand All @@ -18,6 +18,8 @@ This design approach leads to the following benefits:

- A well-designed React compound component should have very little state management required.

<div className="h-5" />

## HTML example

A similiar example in HTML are the `<select>` and `<option>` tags:
Expand All @@ -32,9 +34,11 @@ A similiar example in HTML are the `<select>` and `<option>` tags:

The select tag works together with the option tag which is used for a drop-down menu to select items in HTML. Here the `<select>` manages the state of the UI, then the `<option>` elements are configured on how the `<select>` should work.

<div className="h-5" />

## Example 1: Building a compound component

<Link to="/compoundComponent/preview/BreadcrumbsExample">view demo</Link>
<DemoLink destination="/compoundComponent/preview/BreadcrumbsExample" />

E-commerce websites often have “breadcrumbs” to help the user navigate to parent/grand-parent pages. For example:

Expand All @@ -43,11 +47,13 @@ E-commerce websites often have “breadcrumbs” to help the user navigate to pa
---

<BreadcrumbDoc />
<div style={{ height: 100 }} />

## Example 2: Compound component with Context

<Link to="/compoundComponent/preview/TabsExample">view demo</Link>
<DemoLink destination="/compoundComponent/preview/TabsExample" />

By leveraging React context, we can provide the necessary states and functions to child components without exposing them through props, resulting in a cleaner and more maintainable codebase.

<TabsDoc />
<div style={{ height: 300 }} />
4 changes: 1 addition & 3 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
--page-header-height: 56px;
--xx: 4px;

--page-side-nav-absolute-width: 240px;
--page-side-nav-absolute-width: 280px;

--page-side-nav-width: calc(
var(--page-side-nav-absolute-width) + var(--page-padding)
Expand Down Expand Up @@ -139,8 +139,6 @@ h4 {
hyphens: auto;
}



@tailwind base;
@tailwind components;
@tailwind utilities;

0 comments on commit f7d456a

Please sign in to comment.