Skip to content

Commit

Permalink
Merge branch 'main' into feat-quote
Browse files Browse the repository at this point in the history
  • Loading branch information
jensmo98 authored Jun 20, 2024
2 parents 12ddcb8 + d1049d4 commit 5bbc33e
Show file tree
Hide file tree
Showing 30 changed files with 1,185 additions and 417 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ coverage/

# Storybook build files
docs/docs

# Generated css files
packages/react/src/styles/*.css
2 changes: 1 addition & 1 deletion .stylelintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
'stylelint-config-prettier',
],
plugins: ['stylelint-prettier'],
ignoreFiles: ['**/dist/**/**.css'],
ignoreFiles: ['**/dist/**/**.css', '**/styles/**.css'],
rules: {
'prettier/prettier': true,
'declaration-block-no-redundant-longhand-properties': null,
Expand Down
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"lint:style": "stylelint \"packages/**/*.css\"",
"test": "jest",
"generate-component": "cd packages/react && node scripts/generate-component.js",
"generate-css": "esno ./packages/react/scripts/generate-css.ts",
"lerna:version": "lerna version --conventional-commits --no-private",
"lerna:publish": "lerna publish from-package",
"pack-eid": "cd packages/react && node scripts/pack-eid.js"
Expand Down Expand Up @@ -45,6 +46,7 @@
"@svgr/rollup": "^7.0.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@types/fs-extra": "^11.0.2",
"@types/jest": "^29.5.0",
"@types/react": "^18.0.31",
"@types/react-dom": "^18.0.11",
Expand Down Expand Up @@ -96,6 +98,14 @@
"@commitlint/cli": "^17.6.3",
"@commitlint/config-conventional": "^17.6.3",
"@digdir/design-system-tokens": "^0.6.0",
"rollup-plugin-copy": "^3.4.0"
"cssnano": "^6.0.1",
"esno": "^0.17.0",
"fast-glob": "^3.3.1",
"fs-extra": "^11.1.1",
"postcss": "^8.4.30",
"postcss-modules": "^6.0.0",
"react-number-format": "^5.3.1",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-postcss-modules": "^2.1.1"
}
}
27 changes: 27 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.4.5](https://github.com/felleslosninger/tlp-react/compare/@digdir/tlp-react@0.4.4...@digdir/tlp-react@0.4.5) (2024-01-23)

**Note:** Version bump only for package @digdir/tlp-react

## [0.4.4](https://github.com/felleslosninger/tlp-react/compare/@digdir/tlp-react@0.4.3...@digdir/tlp-react@0.4.4) (2023-11-14)

**Note:** Version bump only for package @digdir/tlp-react

## [0.4.3](https://github.com/felleslosninger/tlp-react/compare/@digdir/tlp-react@0.4.2...@digdir/tlp-react@0.4.3) (2023-11-03)

**Note:** Version bump only for package @digdir/tlp-react

## [0.4.2](https://github.com/felleslosninger/tlp-react/compare/@digdir/tlp-react@0.4.1...@digdir/tlp-react@0.4.2) (2023-11-03)

**Note:** Version bump only for package @digdir/tlp-react

## [0.4.1](https://github.com/felleslosninger/tlp-react/compare/@digdir/tlp-react@0.4.0...@digdir/tlp-react@0.4.1) (2023-10-27)

**Note:** Version bump only for package @digdir/tlp-react

# [0.4.0](https://github.com/felleslosninger/tlp-react/compare/@digdir/tlp-react@0.3.1...@digdir/tlp-react@0.4.0) (2023-09-21)

### Features

- **FileCard:** Adding new component FileCard ([#31](https://github.com/felleslosninger/tlp-react/issues/31)) ([8683b87](https://github.com/felleslosninger/tlp-react/commit/8683b87d36136e530c2356e59b32bb4fe6269a63))
- **generate-css:** Generates css files for all module.css files ([#37](https://github.com/felleslosninger/tlp-react/issues/37)) ([92c2a8b](https://github.com/felleslosninger/tlp-react/commit/92c2a8b362d477b6767f287000b7d013d55bac15))

## [0.3.1](https://github.com/felleslosninger/tlp-react/compare/@digdir/tlp-react@0.3.0...@digdir/tlp-react@0.3.1) (2023-08-18)

**Note:** Version bump only for package @digdir/tlp-react
Expand Down
14 changes: 14 additions & 0 deletions packages/react/css-exports.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"modules": [
"Breadcrumb.css",
"Container.css",
"FileCard.css",
"Footer.css",
"Header.css",
"HeaderStories.css",
"LinkList.css",
"LinkPanel.css",
"StorybookLayout.css"
],
"global": "global.css"
}
21 changes: 17 additions & 4 deletions packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
{
"name": "@digdir/tlp-react",
"version": "0.3.1",
"version": "0.4.5",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/index.d.ts",
"sideEffects": false,
"sideEffects": [
"*.css"
],
"license": "MIT",
"author": "Team Løysingar og Portalar",
"files": [
"dist/**"
"dist",
"dist/*.css"
],
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
},
"./dist/*.css": {
"import": "./dist/*.css",
"require": "./dist/*.css"
}
},
"scripts": {
"build": "pnpm clean && tsc -p tsconfig.build.json && rollup -c",
"build": "pnpm clean && esno ./scripts/generate-css.ts && tsc -p tsconfig.build.json && rollup -c",
"clean": "rimraf dist"
},
"peerDependencies": {
Expand Down
39 changes: 25 additions & 14 deletions packages/react/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,34 @@ import copy from 'rollup-plugin-copy';

import terser from './rollup-terser.mjs';
import packageJson from './package.json';
import { generateScopedName } from './rollup/hash-css-name.mjs';

// css files needs to be bundled
const altinnFigmaTokensExceptCss = /@altinn\/figma-design-tokens.*(?<!css)$/;

const plugins = [
peerDepsExternal(),
resolve(),
commonjs(),
json(),
typescript({ tsconfig: './tsconfig.build.json' }),
svgr({ exportType: 'named' }),
postcss({
extract: true,
modules: {
generateScopedName,
},
}),
terser(),
image(),
copy({
targets: [
{ src: 'src/tokens/tokens.css', dest: 'dist' },
{ src: 'src/styles/**/*.css', dest: 'dist/styles' },
],
}),
];

export default [
{
input: 'src/index.ts',
Expand All @@ -37,20 +61,7 @@ export default [
/leaflet/,
/@navikt\/ds-icons/,
],
plugins: [
peerDepsExternal(),
resolve(),
commonjs(),
json(),
typescript({ tsconfig: './tsconfig.build.json' }),
svgr({ exportType: 'named' }),
postcss(),
terser(),
image(),
copy({
targets: [{ src: 'src/tokens/tokens.css', dest: 'dist' }],
}),
],
plugins,
},
{
input: 'dist/types/index.d.ts',
Expand Down
20 changes: 20 additions & 0 deletions packages/react/rollup/hash-css-name.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import path from 'path';

/* eslint-disable no-bitwise */
export function hashCode(input) {
let hash = 0;
for (let i = 0; i < input.length; i += 1) {
const chr = input.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0;
}
return (hash + 2147483648).toString(16);
}

export function generateScopedName(selector, fileName) {
const componentName = path
.basename(fileName)
.replace('.module', '')
.replace('.css', '');
return `tlp-${hashCode(`${componentName}-${selector}`)}`;
}
93 changes: 93 additions & 0 deletions packages/react/scripts/generate-css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import path from 'path';

import postcss from 'postcss';
import cssnano from 'cssnano';
import postcssModules from 'postcss-modules';
import glob from 'fast-glob';
import fs from 'fs-extra';

import { generateScopedName } from '../rollup/hash-css-name.mjs';

console.log('Generating css files');

console.log({
dirPath: path.resolve(__dirname, '../src/**/*.css').replace(/\\/g, '/'),
});

const files = glob.sync(
path.resolve(__dirname, '../src/**/*.css').replace(/\\/g, '/'),
);
const modules = files.filter((file) => file.endsWith('.module.css'));
const global = files.find((file) => file.endsWith('global.css'));

if (typeof global !== 'string') {
throw new Error('Could not find global.css file');
}

console.log({
allCssFiles: files,
allCssModules: modules,
globalCssFile: global,
});

function prepareFileName(filePath: string) {
return path.basename(filePath).replace('.module.css', '.css');
}

fs.writeJsonSync(
path.join(__dirname, '../css-exports.json'),
{ modules: modules.map(prepareFileName), global: prepareFileName(global) },
{ spaces: 2 },
);

const outputFolder = path.resolve(__dirname, '../src/styles');
fs.ensureDirSync(outputFolder);

async function processFile(
filePath: string,
scopeBehaviour: 'local' | 'global',
) {
const result = await postcss([
postcssModules({
generateScopedName,
getJSON: () => {
return;
},
scopeBehaviour,
}),
cssnano({ preset: 'default' }),
]).process(fs.readFileSync(filePath, 'utf-8'), {
from: path.basename(filePath),
});

const fileName = prepareFileName(filePath);
return fs.writeFile(path.join(outputFolder, fileName), result.css);
}

async function createFiles() {
if (typeof global !== 'string') {
throw new Error('Could not find global.css file');
}

await processFile(global, 'global');
await Promise.all(modules.map((file) => processFile(file, 'local')));
await concatIntoGlobal();

console.log('Done generating css files');
}

async function concatIntoGlobal() {
const cssFiles = glob.sync(
path.resolve(__dirname, '../src/styles**/*.css').replace(/\\/g, '/'),
);
console.log({ generatedCssFiles: cssFiles });
const cssFilesContent = cssFiles.map((file) =>
fs.readFileSync(file, 'utf-8'),
);
fs.writeFileSync(
path.join(outputFolder, 'global.css'),
cssFilesContent.join('\n'),
);
}

createFiles();
21 changes: 0 additions & 21 deletions packages/react/src/components/Breadcrumb/Breadcrumb.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,6 @@ describe('Breadcrumb', () => {
jest.spyOn(console, 'error').mockImplementation(jest.fn());
expect(renderFn).toThrow('You must use one or more valid ReactNodes');
});

it('render seperator icon', () => {
render({
children: [
<Link
key={1}
href='#'
>
Home
</Link>,
<Link
key={2}
href='#'
>
Home1
</Link>,
],
});
const icon = screen.getByRole('img');
expect(icon.classList).toContain('seperator');
});
});

const render = (props: BreadcrumbProps) =>
Expand Down
10 changes: 7 additions & 3 deletions packages/react/src/components/Breadcrumb/Breadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import classes from './Breadcrumb.module.css';
interface BreadcrumbProps {
/**Array of children, that vil be rendered in the component */
children: Array<React.ReactNode>;
className?: string;
}

const Breadcrumb = ({ children }: BreadcrumbProps) => {
const Breadcrumb = ({ children, className }: BreadcrumbProps) => {
const breadcrumbElements = React.Children.map(children, (child, index) => {
if (children.length > 0 && React.isValidElement(child)) {
return (
Expand All @@ -23,7 +24,10 @@ const Breadcrumb = ({ children }: BreadcrumbProps) => {
...child.props,
})}
{index === children.length - 1 ? null : (
<ChevronRightIcon className={cn(classes.seperator)} />
<ChevronRightIcon
className={cn(classes.seperator)}
aria-hidden='true'
/>
)}
</li>
);
Expand All @@ -34,7 +38,7 @@ const Breadcrumb = ({ children }: BreadcrumbProps) => {
});

return (
<nav className={cn(classes.wrapper)}>
<nav className={cn(classes.wrapper, className)}>
<ol className={cn(classes.breadcrumbList)}>{breadcrumbElements}</ol>
</nav>
);
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/components/FileCard/FileCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const FileCard = ({
>
<div className={cn(classes.fileIcon)}>{icon ? icon : <FileIcon />}</div>
<div className={cn(classes.fileText)}>
{/* This component fails on server side because of Heading and Paragraph */}
<Heading size='xxsmall'>{heading}</Heading>
<Paragraph size='small'>{description}</Paragraph>
</div>
Expand Down
5 changes: 5 additions & 0 deletions packages/react/src/components/Footer/Footer.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
flex-wrap: wrap;
}

.fewChilds {
justify-content: flex-start;
gap: 25%;
}

.column {
max-width: 33.3333%;
flex: 0 0 auto;
Expand Down
Loading

0 comments on commit 5bbc33e

Please sign in to comment.