Skip to content

Commit

Permalink
feat: add core-config
Browse files Browse the repository at this point in the history
  • Loading branch information
reme3d2y committed Oct 24, 2024
1 parent 5882405 commit 0b1e8f9
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 6 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/core-config-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Publish core-config

on:
workflow_dispatch:
inputs:
version:
description: 'Введите версию x.x.x'
required: true

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Check permissions
if: ${{ contains(fromJSON('["reme3d2y","Valeri8888","SiebenSieben","fulcanellee"]'), github.actor) == false }}
uses: actions/github-script@v6
with:
script: |
core.setFailed("you don't have permission to run this workflow!");
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'

- name: Install dependencies
run: yarn --pure-lockfile

- name: Build app
run: |
cd ./external/core-config
yarn build
- name: Set version
if: success()
run: |
cd ./external/core-config
git config user.name core-ds-bot
git config user.email [email protected]
yarn --new-version version ${{ github.event.inputs.version }}
git push
- name: Publish
if: success()
run: |
cd ./external/core-config
npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
21 changes: 21 additions & 0 deletions external/core-config/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 core-ds

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Empty file added external/core-config/README.md
Empty file.
17 changes: 17 additions & 0 deletions external/core-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@alfalab/core-config",
"version": "0.0.0",
"description": "Глобальный конфиг для настройки core-components",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"files": [
"/dist"
],
"scripts": {
"build": "tsc"
}
}
26 changes: 26 additions & 0 deletions external/core-config/src/CoreConfigContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createContext, useContext } from 'react';

export type CoreConfigContext = {
breakpoint: number;
ssrView: 'desktop' | 'mobile';
};

export const CoreConfigContext = createContext<CoreConfigContext>({
breakpoint: 1024,
ssrView: 'desktop',
});

export const useCoreConfig = (overrides: Partial<CoreConfigContext> = {}) => {
const config = useContext(CoreConfigContext);

Object.entries(overrides).forEach(([key, value]) => {
if (value === undefined) {
delete overrides[key as keyof typeof overrides];
}
});

return {
...config,
...overrides,
};
};
1 change: 1 addition & 0 deletions external/core-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CoreConfigContext';
14 changes: 14 additions & 0 deletions external/core-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es2016",
"jsx": "react",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"declarationDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
15 changes: 9 additions & 6 deletions packages/button/src/Component.responsive.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import React, { forwardRef } from 'react';

import { useMatchMedia } from '@alfalab/core-components-mq';
import { getComponentBreakpoint } from '@alfalab/core-components-shared';
import { useIsDesktop } from '@alfalab/core-components-mq';

import { ButtonDesktop } from './desktop';
import { ButtonMobile } from './mobile';
import { ButtonProps } from './typings';

export const Button = forwardRef<HTMLAnchorElement | HTMLButtonElement, ButtonProps>(
(
{ children, breakpoint = getComponentBreakpoint(), defaultMatchMediaValue, ...restProps },
{
children,
breakpoint,
ssrView,
defaultMatchMediaValue = ssrView !== undefined ? ssrView === 'desktop' : undefined,
...restProps
},
ref,
) => {
const query = `(min-width: ${breakpoint}px)`;

const [isDesktop] = useMatchMedia(query, defaultMatchMediaValue);
const isDesktop = useIsDesktop(breakpoint, defaultMatchMediaValue);

const Component = isDesktop ? ButtonDesktop : ButtonMobile;

Expand Down
6 changes: 6 additions & 0 deletions packages/button/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,14 @@ export type ButtonProps = CommonButtonProps & {
*/
breakpoint?: number;

/**
* Версия, которая будет использоваться при серверном рендеринге
*/
ssrView?: 'desktop' | 'mobile';

/**
* Значение по-умолчанию для хука useMatchMedia
* @deprecated Используйте ssrView
*/
defaultMatchMediaValue?: boolean | (() => boolean);
};
1 change: 1 addition & 0 deletions packages/mq/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './Component';
export * from './useMatchMedia';
export * from './useIsDesktop';
20 changes: 20 additions & 0 deletions packages/mq/src/useIsDesktop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useCoreConfig } from '@alfalab/core-config';
import { useMatchMedia } from './useMatchMedia';

export function useIsDesktop(breakpoint?: number, defaultValue?: boolean | (() => boolean)) {
let ssrView;

if (typeof defaultValue === 'boolean') {
ssrView = defaultValue ? 'desktop' : 'mobile';
} else if (typeof defaultValue === 'function') {
ssrView = defaultValue() ? 'desktop' : 'mobile';
}

const config = useCoreConfig({ breakpoint, ssrView });

const query = `(min-width: ${config.breakpoint}px)`;

const [isDesktop] = useMatchMedia(query, config.ssrView === 'desktop');

return isDesktop;
}

0 comments on commit 0b1e8f9

Please sign in to comment.