Skip to content

Commit

Permalink
✨ : add a dimension helper
Browse files Browse the repository at this point in the history
  • Loading branch information
hsunpei committed Oct 5, 2024
1 parent 9e90ccc commit 9421238
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/layout/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
extends: '../../configs/.eslintrc.js',
rules: {
// https://github.com/pmndrs/react-three-fiber/discussions/2487
"react/no-unknown-property": ["off", { "ignore": ["JSX"] }],
}
};
8 changes: 8 additions & 0 deletions packages/layout/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## 0.0.1 (2024-09-29)

**Note:** Version bump only for package @react-scrollytelling/core
11 changes: 11 additions & 0 deletions packages/layout/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `core`

> TODO: description
## Usage

```
const core = require('core');
// TODO: DEMONSTRATE API
```
197 changes: 197 additions & 0 deletions packages/layout/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/

import type { Config } from 'jest';

const config: Config = {
// All imported modules in your tests should be mocked automatically
// automock: false,

// Stop running tests after `n` failures
// bail: 0,

// The directory where Jest should store its cached dependency information
// cacheDirectory: "/private/var/folders/bm/2l_xg8xj60gc5r1w3_kx6yhc0000gp/T/jest_dy",

// Automatically clear mock calls, instances, contexts and results before every test
clearMocks: true,

// Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false,

// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,

// The directory where Jest should output its coverage files
// coverageDirectory: undefined,

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],

// Indicates which provider should be used to instrument code for coverage
coverageProvider: 'v8',

// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],

// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,

// A path to a custom dependency extractor
// dependencyExtractor: undefined,

// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,

// The default configuration for fake timers
// fakeTimers: {
// "enableGlobally": false
// },

// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],

// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: undefined,

// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,

// A set of global variables that need to be available in all test environments
// globals: {},

// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",

// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// "node_modules"
// ],

// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "mjs",
// "cjs",
// "jsx",
// "ts",
// "tsx",
// "json",
// "node"
// ],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],

// Activates notifications for test results
// notify: false,

// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
preset: 'ts-jest',

// Run tests from one or more projects
// projects: undefined,

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,

// Automatically reset mock state before every test
// resetMocks: false,

// Reset the module registry before running each individual test
// resetModules: false,

// A path to a custom resolver
// resolver: undefined,

// Automatically restore mock state and implementation before every test
// restoreMocks: false,

// The root directory that Jest should scan for tests and modules within
// rootDir: undefined,

// A list of paths to directories that Jest should use to search for files in
// roots: [
// "<rootDir>"
// ],

// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",

// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],

// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],

// The test environment that will be used for testing
testEnvironment: 'jsdom',

// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},

// Adds a location field to test results
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
testPathIgnorePatterns: ['/node_modules/', '/dist/'],

// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],

// This option allows the use of a custom results processor
// testResultsProcessor: undefined,

// This option allows use of a custom test runner
// testRunner: "jest-circus/runner",

// A map from regular expressions to paths to transformers
// transform: undefined,

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
// "/node_modules/",
// "\\.pnp\\.[^\\/]+$"
// ],

// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,

// Indicates whether each individual test should be reported during the run
// verbose: undefined,

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],

// Whether to use watchman for file crawling
// watchman: true,
};

export default config;
49 changes: 49 additions & 0 deletions packages/layout/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@react-scrollytelling/layout",
"version": "0.0.1",
"description": "> TODO: description",
"author": "@hsunpei",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"homepage": "https://github.com/hsunpei/react-scrollytelling#readme",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
"types": "./dist/src/index.d.ts",
"sideEffects": false,
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/hsunpei/react-scrollytelling.git"
},
"scripts": {
"dev": "BUILD_MODE=dev yarn dev:dist",
"dev:dist": "yarn build:dist --watch",
"dev:declarations": "tsc -p . --emitDeclarationOnly --watch --outDir dist_types",
"build": "run-s build:dist build:declarations",
"build:dist": "vite build",
"build:declarations": "tsc -p . --emitDeclarationOnly --outDir dist_types",
"clean": "rimraf -rf ./dist && rimraf -rf ./dist_types",
"test": "jest",
"test:watch": "jest --watch"
},
"bugs": {
"url": "https://github.com/hsunpei/react-scrollytelling/issues"
},
"devDependencies": {
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.4.8",
"@testing-library/react": "^16.0.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"ts-jest": "^29.2.4",
"vite": "^5.2.11"
},
"peerDependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
1 change: 1 addition & 0 deletions packages/layout/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useResizeObserver';
38 changes: 38 additions & 0 deletions packages/layout/src/hooks/useResizeObserver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useEffect, useRef, useState } from 'react';

export function useResizeObserver(ref: React.RefObject<Element>) {
const resizeObserver = useRef<ResizeObserver | null>(null);

const [sizes, setSizes] = useState<DOMRectReadOnly>({
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0,
x: 0,
y: 0,
toJSON: () => null,
});

useEffect(() => {
if (!resizeObserver.current) {
resizeObserver.current = new ResizeObserver((entries) => {
const [entry] = entries;
setSizes(entry.contentRect);
});
}

const { current: observer } = resizeObserver;

if (ref.current) {
observer?.observe(ref.current);
}

return () => {
observer?.disconnect();
};
}, [ref]);

return sizes;
}
1 change: 1 addition & 0 deletions packages/layout/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './hooks';
9 changes: 9 additions & 0 deletions packages/layout/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../configs/tsconfig.libraries.json",
"include": ["src/**/*", "src/**/*.json"],
"compilerOptions": {
// temporarily skip lib check until we can fix the errors from react-three-fiber
// https://github.com/pmndrs/react-three-fiber/issues/2501
"skipLibCheck": true
},
}
19 changes: 19 additions & 0 deletions packages/layout/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { resolve } from 'path';

import { defineConfig, mergeConfig } from 'vite';

import pkg from './package.json';
import { getDefaultViteConfig } from '../../configs/vite.default.config';

// https://vitejs.dev/config/
export default defineConfig(({ command }) =>
mergeConfig(getDefaultViteConfig(pkg, command), {
build: {
lib: {
entry: resolve(__dirname, 'src/index.ts'),
},
target: command === 'serve' ? 'modules' : 'es2015',
// custom vite config here
},
})
);

0 comments on commit 9421238

Please sign in to comment.