@obosbbl/grunnmuren-react 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +211 -0
- package/dist/index.d.mts +596 -0
- package/dist/index.mjs +1945 -0
- package/package.json +23 -21
- package/dist/Button/Button.d.mts +0 -71
- package/dist/Button/Button.d.ts +0 -71
- package/dist/Button/Button.mjs +0 -86
package/README.md
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# @obosbbl/grunnmuren-react
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@obosbbl/grunnmuren-react)
|
|
4
|
+
|
|
5
|
+
Grunnmuren React components.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
# npm
|
|
11
|
+
npm install @obosbbl/grunnmuren-react@canary
|
|
12
|
+
|
|
13
|
+
# pnpm
|
|
14
|
+
pnpm add @obosbbl/grunnmuren-react@canary
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
### Internationalization
|
|
20
|
+
|
|
21
|
+
Grunnmuren uses [React Aria Components](https://react-spectrum.adobe.com/react-aria/) under the hood. RAC has built in translation strings for non visible content (for accessibility reasons). It also automatically detects the language based on the browser or system language.
|
|
22
|
+
|
|
23
|
+
To ensure that the language of the page content matches the accessibility strings you must wrap your application in a `GrunnmurenProvider` with a `locale` prop. This will override RAC's automatic locale selection.
|
|
24
|
+
|
|
25
|
+
In [Next.js](https://nextjs.org/) you can do this in the root [root layout](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#root-layout-required). In order to avoid making `RootLayout` a client component, you should import `GrunnmurenProvider` in a providers-file, that uses `"use client"`
|
|
26
|
+
|
|
27
|
+
Valid locales are `nb`, `sv` or `en`. The provider defaults to `nb` if unspecified.
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
// app/providers.tsx
|
|
31
|
+
'use client'
|
|
32
|
+
import { GrunnmurenProvider } from '@obosbbl/grunnmuren-react';
|
|
33
|
+
|
|
34
|
+
export function Providers({children, locale}: { children: React.ReactNode, locale: 'nb' | 'sv' | 'en'}) {
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<GrunnmurenProvider locale={locale}>
|
|
38
|
+
{children}
|
|
39
|
+
</GrunnmurenProvider>
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
// app/layout.tsx
|
|
46
|
+
|
|
47
|
+
export default function RootLayout({
|
|
48
|
+
children,
|
|
49
|
+
}: {
|
|
50
|
+
children: React.ReactNode
|
|
51
|
+
}) {
|
|
52
|
+
|
|
53
|
+
// Either 'nb', 'sv' or 'en'
|
|
54
|
+
const locale = 'nb';
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<Providers locale={locale}>
|
|
58
|
+
<html lang={locale}>
|
|
59
|
+
<body>{children}</body>
|
|
60
|
+
</html>
|
|
61
|
+
</Providers>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
See the [RAC internationalization docs](https://react-spectrum.adobe.com/react-aria/internationalization.html) for more information.
|
|
67
|
+
|
|
68
|
+
### Routing
|
|
69
|
+
|
|
70
|
+
When using compontents that include links from RAC (For example `Breadcrumbs`), the links will always treat the hrefs as external.
|
|
71
|
+
|
|
72
|
+
In order to avoid hard refreshing, you need to prop your router navigation-function
|
|
73
|
+
through `GrunnmurenProvider`. See the [RAC routing docs](https://react-spectrum.adobe.com/react-aria/routing.html)
|
|
74
|
+
|
|
75
|
+
In [Next.js](https://nextjs.org/) this is also done in the root [root layout](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts#root-layout-required). In order to avoid making `RootLayout` a client component, you should import `GrunnmurenProvider` in a providers-file, that uses `"use client"`
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
// app/providers.tsx
|
|
79
|
+
'use client'
|
|
80
|
+
import { GrunnmurenProvider } from '@obosbbl/grunnmuren-react';
|
|
81
|
+
import { useRouter } from 'next/navigation';
|
|
82
|
+
|
|
83
|
+
export function Providers({children, locale}: { children: React.ReactNode, locale: string}) {
|
|
84
|
+
const router = useRouter();
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<GrunnmurenProvider locale={locale} navigate={router.push}>
|
|
88
|
+
{children}
|
|
89
|
+
</GrunnmurenProvider>
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The `RootLayout` file then looks exactly like it does in the previous step:
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
// app/layout.tsx
|
|
98
|
+
import {Providers} from "./providers";
|
|
99
|
+
|
|
100
|
+
export default function RootLayout({
|
|
101
|
+
children,
|
|
102
|
+
}: {
|
|
103
|
+
children: React.ReactNode
|
|
104
|
+
}) {
|
|
105
|
+
|
|
106
|
+
// Either 'nb', 'sv' or 'en'
|
|
107
|
+
const locale = 'nb';
|
|
108
|
+
|
|
109
|
+
return (
|
|
110
|
+
<Providers locale={locale}>
|
|
111
|
+
<html lang={locale}>
|
|
112
|
+
<body>{children}</body>
|
|
113
|
+
</html>
|
|
114
|
+
</Providers>
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### Basepath
|
|
120
|
+
|
|
121
|
+
If you're using a router such as Next's, then you can use the `useHref` prop to convert router-specific hrefs into native HTML hrefs. This is very useful for instance when using Next's [basepath](https://nextjs.org/docs/app/api-reference/next-config-js/basePath) setting, as you can use this to prepend the basepath to all links, similar to Next's `<Link>`.
|
|
122
|
+
|
|
123
|
+
**Before**
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
import Link from 'next/link';
|
|
127
|
+
import { Button } from '@obosbbl/grunnmuren-react';
|
|
128
|
+
|
|
129
|
+
// Notice how you have to handle the basepath yourself with Grunnmuren's component, but not with Next's.
|
|
130
|
+
|
|
131
|
+
<Link href="/bli-medlem">Bli medlem</Link>
|
|
132
|
+
<Button href="/medlem/bli-medlem">Bli medlem</Button>
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
**After**
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
// app/providers.tsx
|
|
139
|
+
'use client'
|
|
140
|
+
import { GrunnmurenProvider } from '@obosbbl/grunnmuren-react';
|
|
141
|
+
import { useRouter } from 'next/navigation';
|
|
142
|
+
|
|
143
|
+
export function Providers({children, locale}: { children: React.ReactNode, locale: string}) {
|
|
144
|
+
const router = useRouter();
|
|
145
|
+
const useHref = (href: string) => '/medlem' + href;
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<GrunnmurenProvider locale={locale} navigate={router.push} useHref={useHref}>
|
|
149
|
+
{children}
|
|
150
|
+
</GrunnmurenProvider>
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
```tsx
|
|
156
|
+
import Link from 'next/link';
|
|
157
|
+
import { Button } from '@obosbbl/grunnmuren-react';
|
|
158
|
+
|
|
159
|
+
// The hrefs are the same, as basepath is handled by the useHref hook in the provider.
|
|
160
|
+
|
|
161
|
+
<Link href="/bli-medlem">Bli medlem</Link>
|
|
162
|
+
<Button href="/bli-medlem">Bli medlem</Button>
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Optimize bundle size by removing unused locales
|
|
166
|
+
|
|
167
|
+
React Aria Components has built in support for over 30 languages, most of which will be unused in your application. To optimize your applications bundle size, it is recommended to use React Aria's build plugin to remove all the unused locales. Here is a quick example for Next.js:
|
|
168
|
+
|
|
169
|
+
#### Install
|
|
170
|
+
|
|
171
|
+
```sh
|
|
172
|
+
# npm
|
|
173
|
+
npm install @react-aria/optimize-locales-plugin --save-dev
|
|
174
|
+
|
|
175
|
+
# pnpm
|
|
176
|
+
pnpm add -D @react-aria/optimize-locales-plugin
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### Configuration
|
|
180
|
+
|
|
181
|
+
```js
|
|
182
|
+
// next.config.js
|
|
183
|
+
const optimizeLocales = require('@react-aria/optimize-locales-plugin');
|
|
184
|
+
|
|
185
|
+
module.exports = {
|
|
186
|
+
webpack(config) {
|
|
187
|
+
config.plugins.push(
|
|
188
|
+
optimizeLocales.webpack({
|
|
189
|
+
// If you have a multitenant app, include both Norwegian and Swedish
|
|
190
|
+
// If your app only serves one language, adjust accordingly
|
|
191
|
+
locales: ['nb', 'sv'],
|
|
192
|
+
}),
|
|
193
|
+
);
|
|
194
|
+
return config;
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
The plugin works with several different bundlers. See [React Aria's bundle size optimization docs](https://react-spectrum.adobe.com/react-aria/internationalization.html#optimizing-bundle-size) for more information.
|
|
200
|
+
|
|
201
|
+
## Usage
|
|
202
|
+
|
|
203
|
+
Before you start using the components you need to configure the [Tailwind preset](../tailwind/). Remember to add this package to the content scan.
|
|
204
|
+
|
|
205
|
+
```js
|
|
206
|
+
import { Button } from '@obosbbl/grunnmuren-react';
|
|
207
|
+
|
|
208
|
+
export default function () {
|
|
209
|
+
return <Button>Click me</Button>;
|
|
210
|
+
}
|
|
211
|
+
```
|