Skip to content

Commit

Permalink
feat: ink-mouse; add mouse event handling to ink components
Browse files Browse the repository at this point in the history
  • Loading branch information
airtonix committed Jun 24, 2024
1 parent d388178 commit aa35b2b
Show file tree
Hide file tree
Showing 34 changed files with 1,213 additions and 877 deletions.
29 changes: 0 additions & 29 deletions .github/actions/setup-npm-publish/action.yml

This file was deleted.

21 changes: 0 additions & 21 deletions .github/actions/setup-npm-publish/create-npmrc.sh

This file was deleted.

6 changes: 0 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ jobs:
- name: Yarn
uses: ./.github/actions/setup-yarn

- name: Configure Publishing
uses: ./.github/actions/setup-npm-publish
with:
NpmRegistryUrl: https://registry.npmjs.org/
NpmRcFile: .npmrc

- name: Preflight
run: |
just lint
Expand Down
Binary file added .yarn/install-state.gz
Binary file not shown.
3 changes: 2 additions & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ enableGlobalCache: false
nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-4.0.2.cjs
npmAlwaysAuth: true
npmAuthToken: "${NPM_AUTH_TOKEN:-}"
npmAuthToken: "${NPM_AUTH_TOKEN:-''}"
npmRegistryServer: 'https://registry.npmjs.org'
106 changes: 95 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,106 @@
# reactink-mouse
# ink-mouse

Your description
Provides mouse support for [Ink](http://github.com/vadimdemedes/ink) components.

## Features

- Mouse position tracking
- Mouse hover tracking
- Mouse click tracking
- Mouse drag tracking
- Element position tracking

[Todo](#todo)

## Usage

```
npm install reactink-mouse
```
```tsx
import React, { useMemo, useRef, useState } from 'react';
import type { ComponentProps } from 'react';
import { Box, DOMElement, Text, render } from 'ink';
import {
MouseProvider,
useOnMouseHover,
useMousePosition,
useElementPosition,
useOnMouseClick,
} from '@zenobius/ink-mouse';

function App() {
return (
<MouseProvider>
<MyComponent />
</MouseProvider>
);
}

function MyComponent() {
const mouse = useMousePosition();

return (
<Box>
<Button label="Button 1" />
<Text>
{mouse.x}, {mouse.y}
</Text>
</Box>
);
}

function Button({ label, onClick }: { label: string; onClick?: () => void }) {
const ref = useRef<DOMElement>(null);

````ts
import { package_name/@/ } from 'package_name/@/';
````
const [hovering, setHovering] = useState(false);
const [clicking, setClicking] = useState(false);

useOnMouseClick(ref, (event) => {
setClicking(event);
if (event && typeof onClick === 'function') {
onClick();
}
});
useOnMouseHover(ref, setHovering);

const border = useMemo((): ComponentProps<typeof Box>['borderStyle'] => {
if (clicking) {
return 'double';
}

if (hovering) {
return 'singleDouble';
}

return 'single';
}, [clicking, hovering]);

return (
<Box
gap={1}
paddingX={1}
ref={ref}
borderStyle={border}
>
<Text>{label}</Text>
</Box>
);
}

render(<App />);
```

<!--- @@inject: dist/docs/modules.md#Functions --->

<!--- @@inject-end: dist/docs/modules.md#Functions --->

<!--- @@inject: dist/docs/interfaces/*.md#Properties --->
REPLACE THIS BLOCK WITH AS MANY REFERENCE TO GENERATED INTERFACE DOCUMENTATION AS YOU NEED
<!--- @@inject-end: dist/docs/interfaces/*.md#Properties --->
## Todo

- [ ] Add tests.
- testing a device may be difficult; but the implementation is sufficiently abstracted from the device that it should be possible to mock the device input stream.
- [ ] Add Drag examples
- Not sure if Ink supports z layers, but it would be nice to have a draggable element that can be dragged over other elements.
- [ ] Research Windows support
- Apparently old CMD.exe only supports [rudimentary ansii escape codes for colours](https://ss64.com/nt/syntax-ansi.html).
- New Windows Terminal does support ansi escape codes, so we'd have to explore what works and what doesn't.
- We might have to fall back to GPM or some other library. Seems a bit complex. want to avoid it if possible.
- [ ] Add support for right and middle click.
- I think these are supported by the terminal, but I'm not sure how to detect them. Is it lowercase M and R?
12 changes: 9 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ types:
./tsconfig.json

unittest:
@vitest --dir ./src
@vitest \
--dir ./src

integrationtest:
tsx e2e/produces-rendered-svg.test.ts
@echo 0

dev:
@tsx \
--watch \
./src/demo/index.tsx

build:
@tsup \
Expand All @@ -40,7 +46,7 @@ docs:
--plugin typedoc-plugin-markdown \
--tsconfig ./tsconfig.lib.json \
--out dist/docs \
./src/remark-nomnoml.ts \
./src/ink-mouse.ts \
--hideBreadcrumbs true \
--namedAnchors true \
--disableSources \
Expand Down
27 changes: 16 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"name": "reactink-mouse",
"name": "@zenobius/ink-mouse",
"version": "0.0.1",
"type": "module",
"dependencies": {
"hast-util-from-html-isomorphic": "2.0.0",
"hast-util-to-html": "^9.0.0",
"hast-util-to-string": "^3.0.0",
"hastscript": "^9.0.0",
"ink": "^5.0.1",
"mdast": "3.0.0",
"nomnoml": "1.6.2",
"react": "^18.2.0",
"tslib": "^2.3.0",
"unified": "11.0.4",
"unist-util-visit-parents": "6.0.1",
Expand All @@ -17,11 +20,10 @@
"@arethetypeswrong/cli": "^0.15.3",
"@swc/core": "^1.6.1",
"@types/is-ci": "^3",
"@types/mock-fs": "^4.13.4",
"@types/node": "18.16.9",
"@types/react": "^18.0.32",
"@typescript-eslint/eslint-plugin": "^7.13.1",
"@typescript-eslint/parser": "^7.13.1",
"dedent": "^1.5.3",
"eslint": "~8.56.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-typescript": "^3.6.1",
Expand All @@ -34,7 +36,6 @@
"lint-staged": "^15.2.7",
"open": "^10.1.0",
"prettier": "^3.2.2",
"puppeteer": "^22.11.2",
"remark": "15.0.1",
"ts-node": "10.9.1",
"tslib": "^2.3.0",
Expand All @@ -47,13 +48,13 @@
"vite": "^5.3.1",
"vitest": "^1.6.0"
},
"main": "./dist/package_name/@/.js",
"types": "./dist/package_name/@/.d.ts",
"main": "./dist/ink-mouse/.js",
"types": "./dist/ink-mouse/.d.ts",
"exports": {
".": {
"import": "./dist/package_name/@/.js",
"require": "./dist/package_name/@/.js",
"types": "./dist/package_name/@/.d.ts"
"import": "./dist/ink-mouse/.js",
"require": "./dist/ink-mouse/.js",
"types": "./dist/ink-mouse/.d.ts"
}
},
"files": [
Expand All @@ -68,6 +69,10 @@
"keywords": [],
"author": {
"name": "Zeno Jiricek",
"email": "zenobi-us@users.noreply.github.com"
"email": "airtonix@users.noreply.github.com"
},
"repository": {
"type": "github",
"url": "http://github.com/zenobi-us/ink-mouse"
}
}
}
8 changes: 0 additions & 8 deletions src/<EXPORT NAME>.spec.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/<EXPORT NAME>.ts

This file was deleted.

Loading

0 comments on commit aa35b2b

Please sign in to comment.