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

fix: Properly setup library exports and fix CJS build #70

Merged
merged 6 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Fixed

- Properly export ESM/CJS library depending on current environment and fix CJS build

## [1.0.10] - 2024-02-05

### Added
Expand Down
2 changes: 1 addition & 1 deletion docs/installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ library:
- Import the Aragon ODS stylesheet bundle in your project entry file (e.g. `src/index.js` or `App.js`):

```typescript
import '@aragon/ods/bundle.css';
import '@aragon/ods/build.css';
```

# Usage
Expand Down
18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
"classnames": "^2.0.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-imask": "^7.3.0",
"react-merge-refs": "^2.0.0"
"react-imask": "^7.3.0"
},
"peerDependencies": {
"@tailwindcss/typography": "^0.5.0",
Expand Down Expand Up @@ -121,5 +120,20 @@
"engines": {
"node": ">=18",
"npm": "please-use-yarn"
},
"exports": {
".": {
"import": {
"types": "./dist/types/src/index.d.ts",
"default": "./dist/index.es.js"
},
"require": {
"types": "./dist/types/src/index.d.ts",
"default": "./dist/index.cjs.js"
}
},
"./index.css": "./index.css",
"./build.css": "./build.css",
"./tailwind.config": "./tailwind.config.js"
}
}
4 changes: 2 additions & 2 deletions src/components/input/inputDate/inputDate.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { fireEvent, render, screen, within } from '@testing-library/react';
import React from 'react';
import * as MergeRefs from 'react-merge-refs';
import * as Utils from '../../../utils';
import { IconType } from '../../icon';
import { InputDate, type IInputDateProps } from './inputDate';

describe('<InputDate /> component', () => {
const useRefMock = jest.spyOn(React, 'useRef');
const mergeRefMock = jest.spyOn(MergeRefs, 'mergeRefs');
const mergeRefMock = jest.spyOn(Utils, 'mergeRefs');

afterEach(() => {
useRefMock.mockReset();
Expand Down
2 changes: 1 addition & 1 deletion src/components/input/inputDate/inputDate.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import classNames from 'classnames';
import { forwardRef, useRef } from 'react';
import { mergeRefs } from 'react-merge-refs';
import { mergeRefs } from '../../../utils';
import { Button } from '../../button';
import { IconType } from '../../icon';
import { useInputProps } from '../hooks';
Expand Down
4 changes: 2 additions & 2 deletions src/components/input/inputTime/inputTime.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { fireEvent, render, screen, within } from '@testing-library/react';
import React from 'react';
import * as MergeRefs from 'react-merge-refs';
import * as Utils from '../../../utils';
import { IconType } from '../../icon';
import { InputTime, type IInputTimeProps } from './inputTime';

describe('<InputTime /> component', () => {
const useRefMock = jest.spyOn(React, 'useRef');
const mergeRefMock = jest.spyOn(MergeRefs, 'mergeRefs');
const mergeRefMock = jest.spyOn(Utils, 'mergeRefs');

afterEach(() => {
useRefMock.mockReset();
Expand Down
2 changes: 1 addition & 1 deletion src/components/input/inputTime/inputTime.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import classNames from 'classnames';
import { forwardRef, useRef } from 'react';
import { mergeRefs } from 'react-merge-refs';
import { mergeRefs } from '../../../utils';
import { Button } from '../../button';
import { IconType } from '../../icon';
import { useInputProps } from '../hooks';
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './formatterUtils';
export * from './mergeRefs';
export * from './responsiveUtils';
1 change: 1 addition & 0 deletions src/utils/mergeRefs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { mergeRefs } from './mergeRefs';
19 changes: 19 additions & 0 deletions src/utils/mergeRefs/mergeRefs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { mergeRefs } from './mergeRefs';

describe('mergeRefs util', () => {
it('correctly set value for function refs', () => {
const functionRef = jest.fn();
const value = 'test';
const refSetter = mergeRefs([functionRef]);
refSetter(value);
expect(functionRef).toHaveBeenCalledWith(value);
});

it('correctly set value for mutable ref object', () => {
const mutableRef = { current: null };
const value = 'new-value';
const refSetter = mergeRefs<string | null>([mutableRef]);
refSetter(value);
expect(mutableRef.current).toEqual(value);
});
});
18 changes: 18 additions & 0 deletions src/utils/mergeRefs/mergeRefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { LegacyRef, MutableRefObject, RefCallback } from 'react';

/**
* Utility to merge multiple React refs, inspired by https://github.com/gregberge/react-merge-refs
*/
export const mergeRefs = <T = unknown>(
refs: Array<MutableRefObject<T> | LegacyRef<T> | undefined | null>,
): RefCallback<T> => {
return (value) => {
refs.forEach((ref) => {
if (typeof ref === 'function') {
ref(value);
} else if (ref != null) {
(ref as MutableRefObject<T | null>).current = value;
}
});
};
};
Loading