Skip to content

Commit

Permalink
feat: add separator prop (#17)
Browse files Browse the repository at this point in the history
Allow the separator between multiple keys to be customizable.
  • Loading branch information
jkoking committed Feb 14, 2023
1 parent aeae615 commit c4a28e9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,29 @@ Use the `combo` dispatched event to listen for a combination of keys.
{combo.join(", ")}
```

#### `separator`

Use the `separator` property to specify the string between key when using `on:combo`

```svelte
<script>
import Keydown from "svelte-keydown";
let combo = [];
</script>
<Keydown separator="+" on:combo={(e) => (combo = [...combo, e.detail])} />
{combo.join(", ")}
```

## API

| Prop | Type | Default value |
| :----------- | :-------- | :------------ |
| paused | `boolean` | `false` |
| pauseOnInput | `boolean` | `false` |
| separator | `string` | `-` |

### Forwarded events

Expand Down
5 changes: 4 additions & 1 deletion src/Keydown.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
/** Set to `true` to pause keydown events when typing in an input field */
export let pauseOnInput = false;
/**Determines the what goes between keys in a combo*/
export let separator = "-";
import { createEventDispatcher } from "svelte";
const dispatch = createEventDispatcher();
let combo = [];
let down = [];
$: combination = combo.join("-");
$: combination = combo.join(separator);
$: comboByKey = combo.reduce((keys, key) => ({ ...keys, [key]: true }), {});
$: if (combo.length > 0) dispatch("combo", combination);
</script>
Expand Down
6 changes: 6 additions & 0 deletions types/Keydown.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export interface KeydownProps {
* @default false
*/
pauseOnInput?: boolean;

/**
* Determines the what goes between keys in a combo*
* @default "-"
*/
separator?: string;
}

export default class Keydown extends SvelteComponentTyped<
Expand Down

0 comments on commit c4a28e9

Please sign in to comment.